src/share/vm/opto/subnode.cpp

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 435
a61af66fc99e
child 468
3288958bf319
permissions
-rw-r--r--

Initial load

     1 /*
     2  * Copyright 1997-2007 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
    49   if( in(1)->Opcode() == Op_AddI ) {
    50     if( phase->eqv(in(1)->in(2),in(2)) )
    51       return in(1)->in(1);
    52     // Also catch: "(X + Opaque2(Y)) - Y".  In this case, 'Y' is a loop-varying
    53     // trip counter and X is likely to be loop-invariant (that's how O2 Nodes
    54     // are originally used, although the optimizer sometimes jiggers things).
    55     // This folding through an O2 removes a loop-exit use of a loop-varying
    56     // value and generally lowers register pressure in and around the loop.
    57     if( in(1)->in(2)->Opcode() == Op_Opaque2 &&
    58         phase->eqv(in(1)->in(2)->in(1),in(2)) )
    59       return in(1)->in(1);
    60   }
    62   return ( phase->type( in(2) )->higher_equal( zero ) ) ? in(1) : this;
    63 }
    65 //------------------------------Value------------------------------------------
    66 // A subtract node differences it's two inputs.
    67 const Type *SubNode::Value( PhaseTransform *phase ) const {
    68   const Node* in1 = in(1);
    69   const Node* in2 = in(2);
    70   // Either input is TOP ==> the result is TOP
    71   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
    72   if( t1 == Type::TOP ) return Type::TOP;
    73   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
    74   if( t2 == Type::TOP ) return Type::TOP;
    76   // Not correct for SubFnode and AddFNode (must check for infinity)
    77   // Equal?  Subtract is zero
    78   if (phase->eqv_uncast(in1, in2))  return add_id();
    80   // Either input is BOTTOM ==> the result is the local BOTTOM
    81   if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
    82     return bottom_type();
    84   return sub(t1,t2);            // Local flavor of type subtraction
    86 }
    88 //=============================================================================
    90 //------------------------------Helper function--------------------------------
    91 static bool ok_to_convert(Node* inc, Node* iv) {
    92     // Do not collapse (x+c0)-y if "+" is a loop increment, because the
    93     // "-" is loop invariant and collapsing extends the live-range of "x"
    94     // to overlap with the "+", forcing another register to be used in
    95     // the loop.
    96     // This test will be clearer with '&&' (apply DeMorgan's rule)
    97     // but I like the early cutouts that happen here.
    98     const PhiNode *phi;
    99     if( ( !inc->in(1)->is_Phi() ||
   100           !(phi=inc->in(1)->as_Phi()) ||
   101           phi->is_copy() ||
   102           !phi->region()->is_CountedLoop() ||
   103           inc != phi->region()->as_CountedLoop()->incr() )
   104        &&
   105         // Do not collapse (x+c0)-iv if "iv" is a loop induction variable,
   106         // because "x" maybe invariant.
   107         ( !iv->is_loop_iv() )
   108       ) {
   109       return true;
   110     } else {
   111       return false;
   112     }
   113 }
   114 //------------------------------Ideal------------------------------------------
   115 Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){
   116   Node *in1 = in(1);
   117   Node *in2 = in(2);
   118   uint op1 = in1->Opcode();
   119   uint op2 = in2->Opcode();
   121 #ifdef ASSERT
   122   // Check for dead loop
   123   if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
   124       ( op1 == Op_AddI || op1 == Op_SubI ) &&
   125       ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
   126         phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1 ) ) )
   127     assert(false, "dead loop in SubINode::Ideal");
   128 #endif
   130   const Type *t2 = phase->type( in2 );
   131   if( t2 == Type::TOP ) return NULL;
   132   // Convert "x-c0" into "x+ -c0".
   133   if( t2->base() == Type::Int ){        // Might be bottom or top...
   134     const TypeInt *i = t2->is_int();
   135     if( i->is_con() )
   136       return new (phase->C, 3) AddINode(in1, phase->intcon(-i->get_con()));
   137   }
   139   // Convert "(x+c0) - y" into (x-y) + c0"
   140   // Do not collapse (x+c0)-y if "+" is a loop increment or
   141   // if "y" is a loop induction variable.
   142   if( op1 == Op_AddI && ok_to_convert(in1, in2) ) {
   143     const Type *tadd = phase->type( in1->in(2) );
   144     if( tadd->singleton() && tadd != Type::TOP ) {
   145       Node *sub2 = phase->transform( new (phase->C, 3) SubINode( in1->in(1), in2 ));
   146       return new (phase->C, 3) AddINode( sub2, in1->in(2) );
   147     }
   148   }
   151   // Convert "x - (y+c0)" into "(x-y) - c0"
   152   // Need the same check as in above optimization but reversed.
   153   if (op2 == Op_AddI && ok_to_convert(in2, in1)) {
   154     Node* in21 = in2->in(1);
   155     Node* in22 = in2->in(2);
   156     const TypeInt* tcon = phase->type(in22)->isa_int();
   157     if (tcon != NULL && tcon->is_con()) {
   158       Node* sub2 = phase->transform( new (phase->C, 3) SubINode(in1, in21) );
   159       Node* neg_c0 = phase->intcon(- tcon->get_con());
   160       return new (phase->C, 3) AddINode(sub2, neg_c0);
   161     }
   162   }
   164   const Type *t1 = phase->type( in1 );
   165   if( t1 == Type::TOP ) return NULL;
   167 #ifdef ASSERT
   168   // Check for dead loop
   169   if( ( op2 == Op_AddI || op2 == Op_SubI ) &&
   170       ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
   171         phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
   172     assert(false, "dead loop in SubINode::Ideal");
   173 #endif
   175   // Convert "x - (x+y)" into "-y"
   176   if( op2 == Op_AddI &&
   177       phase->eqv( in1, in2->in(1) ) )
   178     return new (phase->C, 3) SubINode( phase->intcon(0),in2->in(2));
   179   // Convert "(x-y) - x" into "-y"
   180   if( op1 == Op_SubI &&
   181       phase->eqv( in1->in(1), in2 ) )
   182     return new (phase->C, 3) SubINode( phase->intcon(0),in1->in(2));
   183   // Convert "x - (y+x)" into "-y"
   184   if( op2 == Op_AddI &&
   185       phase->eqv( in1, in2->in(2) ) )
   186     return new (phase->C, 3) SubINode( phase->intcon(0),in2->in(1));
   188   // Convert "0 - (x-y)" into "y-x"
   189   if( t1 == TypeInt::ZERO && op2 == Op_SubI )
   190     return new (phase->C, 3) SubINode( in2->in(2), in2->in(1) );
   192   // Convert "0 - (x+con)" into "-con-x"
   193   jint con;
   194   if( t1 == TypeInt::ZERO && op2 == Op_AddI &&
   195       (con = in2->in(2)->find_int_con(0)) != 0 )
   196     return new (phase->C, 3) SubINode( phase->intcon(-con), in2->in(1) );
   198   // Convert "(X+A) - (X+B)" into "A - B"
   199   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) )
   200     return new (phase->C, 3) SubINode( in1->in(2), in2->in(2) );
   202   // Convert "(A+X) - (B+X)" into "A - B"
   203   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) )
   204     return new (phase->C, 3) SubINode( in1->in(1), in2->in(1) );
   206   // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally
   207   // nicer to optimize than subtract.
   208   if( op2 == Op_SubI && in2->outcnt() == 1) {
   209     Node *add1 = phase->transform( new (phase->C, 3) AddINode( in1, in2->in(2) ) );
   210     return new (phase->C, 3) SubINode( add1, in2->in(1) );
   211   }
   213   return NULL;
   214 }
   216 //------------------------------sub--------------------------------------------
   217 // A subtract node differences it's two inputs.
   218 const Type *SubINode::sub( const Type *t1, const Type *t2 ) const {
   219   const TypeInt *r0 = t1->is_int(); // Handy access
   220   const TypeInt *r1 = t2->is_int();
   221   int32 lo = r0->_lo - r1->_hi;
   222   int32 hi = r0->_hi - r1->_lo;
   224   // We next check for 32-bit overflow.
   225   // If that happens, we just assume all integers are possible.
   226   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
   227        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
   228       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
   229        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
   230     return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen));
   231   else                          // Overflow; assume all integers
   232     return TypeInt::INT;
   233 }
   235 //=============================================================================
   236 //------------------------------Ideal------------------------------------------
   237 Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
   238   Node *in1 = in(1);
   239   Node *in2 = in(2);
   240   uint op1 = in1->Opcode();
   241   uint op2 = in2->Opcode();
   243 #ifdef ASSERT
   244   // Check for dead loop
   245   if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
   246       ( op1 == Op_AddL || op1 == Op_SubL ) &&
   247       ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
   248         phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1  ) ) )
   249     assert(false, "dead loop in SubLNode::Ideal");
   250 #endif
   252   if( phase->type( in2 ) == Type::TOP ) return NULL;
   253   const TypeLong *i = phase->type( in2 )->isa_long();
   254   // Convert "x-c0" into "x+ -c0".
   255   if( i &&                      // Might be bottom or top...
   256       i->is_con() )
   257     return new (phase->C, 3) AddLNode(in1, phase->longcon(-i->get_con()));
   259   // Convert "(x+c0) - y" into (x-y) + c0"
   260   // Do not collapse (x+c0)-y if "+" is a loop increment or
   261   // if "y" is a loop induction variable.
   262   if( op1 == Op_AddL && ok_to_convert(in1, in2) ) {
   263     Node *in11 = in1->in(1);
   264     const Type *tadd = phase->type( in1->in(2) );
   265     if( tadd->singleton() && tadd != Type::TOP ) {
   266       Node *sub2 = phase->transform( new (phase->C, 3) SubLNode( in11, in2 ));
   267       return new (phase->C, 3) AddLNode( sub2, in1->in(2) );
   268     }
   269   }
   271   // Convert "x - (y+c0)" into "(x-y) - c0"
   272   // Need the same check as in above optimization but reversed.
   273   if (op2 == Op_AddL && ok_to_convert(in2, in1)) {
   274     Node* in21 = in2->in(1);
   275     Node* in22 = in2->in(2);
   276     const TypeLong* tcon = phase->type(in22)->isa_long();
   277     if (tcon != NULL && tcon->is_con()) {
   278       Node* sub2 = phase->transform( new (phase->C, 3) SubLNode(in1, in21) );
   279       Node* neg_c0 = phase->longcon(- tcon->get_con());
   280       return new (phase->C, 3) AddLNode(sub2, neg_c0);
   281     }
   282   }
   284   const Type *t1 = phase->type( in1 );
   285   if( t1 == Type::TOP ) return NULL;
   287 #ifdef ASSERT
   288   // Check for dead loop
   289   if( ( op2 == Op_AddL || op2 == Op_SubL ) &&
   290       ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
   291         phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
   292     assert(false, "dead loop in SubLNode::Ideal");
   293 #endif
   295   // Convert "x - (x+y)" into "-y"
   296   if( op2 == Op_AddL &&
   297       phase->eqv( in1, in2->in(1) ) )
   298     return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO), in2->in(2));
   299   // Convert "x - (y+x)" into "-y"
   300   if( op2 == Op_AddL &&
   301       phase->eqv( in1, in2->in(2) ) )
   302     return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO),in2->in(1));
   304   // Convert "0 - (x-y)" into "y-x"
   305   if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL )
   306     return new (phase->C, 3) SubLNode( in2->in(2), in2->in(1) );
   308   // Convert "(X+A) - (X+B)" into "A - B"
   309   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) )
   310     return new (phase->C, 3) SubLNode( in1->in(2), in2->in(2) );
   312   // Convert "(A+X) - (B+X)" into "A - B"
   313   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) )
   314     return new (phase->C, 3) SubLNode( in1->in(1), in2->in(1) );
   316   // Convert "A-(B-C)" into (A+C)-B"
   317   if( op2 == Op_SubL && in2->outcnt() == 1) {
   318     Node *add1 = phase->transform( new (phase->C, 3) AddLNode( in1, in2->in(2) ) );
   319     return new (phase->C, 3) SubLNode( add1, in2->in(1) );
   320   }
   322   return NULL;
   323 }
   325 //------------------------------sub--------------------------------------------
   326 // A subtract node differences it's two inputs.
   327 const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const {
   328   const TypeLong *r0 = t1->is_long(); // Handy access
   329   const TypeLong *r1 = t2->is_long();
   330   jlong lo = r0->_lo - r1->_hi;
   331   jlong hi = r0->_hi - r1->_lo;
   333   // We next check for 32-bit overflow.
   334   // If that happens, we just assume all integers are possible.
   335   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
   336        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
   337       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
   338        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
   339     return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen));
   340   else                          // Overflow; assume all integers
   341     return TypeLong::LONG;
   342 }
   344 //=============================================================================
   345 //------------------------------Value------------------------------------------
   346 // A subtract node differences its two inputs.
   347 const Type *SubFPNode::Value( PhaseTransform *phase ) const {
   348   const Node* in1 = in(1);
   349   const Node* in2 = in(2);
   350   // Either input is TOP ==> the result is TOP
   351   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
   352   if( t1 == Type::TOP ) return Type::TOP;
   353   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
   354   if( t2 == Type::TOP ) return Type::TOP;
   356   // if both operands are infinity of same sign, the result is NaN; do
   357   // not replace with zero
   358   if( (t1->is_finite() && t2->is_finite()) ) {
   359     if( phase->eqv(in1, in2) ) return add_id();
   360   }
   362   // Either input is BOTTOM ==> the result is the local BOTTOM
   363   const Type *bot = bottom_type();
   364   if( (t1 == bot) || (t2 == bot) ||
   365       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
   366     return bot;
   368   return sub(t1,t2);            // Local flavor of type subtraction
   369 }
   372 //=============================================================================
   373 //------------------------------Ideal------------------------------------------
   374 Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
   375   const Type *t2 = phase->type( in(2) );
   376   // Convert "x-c0" into "x+ -c0".
   377   if( t2->base() == Type::FloatCon ) {  // Might be bottom or top...
   378     // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) );
   379   }
   381   // Not associative because of boundary conditions (infinity)
   382   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
   383     // Convert "x - (x+y)" into "-y"
   384     if( in(2)->is_Add() &&
   385         phase->eqv(in(1),in(2)->in(1) ) )
   386       return new (phase->C, 3) SubFNode( phase->makecon(TypeF::ZERO),in(2)->in(2));
   387   }
   389   // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes
   390   // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0.
   391   //if( phase->type(in(1)) == TypeF::ZERO )
   392   //return new (phase->C, 2) NegFNode(in(2));
   394   return NULL;
   395 }
   397 //------------------------------sub--------------------------------------------
   398 // A subtract node differences its two inputs.
   399 const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const {
   400   // no folding if one of operands is infinity or NaN, do not do constant folding
   401   if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) {
   402     return TypeF::make( t1->getf() - t2->getf() );
   403   }
   404   else if( g_isnan(t1->getf()) ) {
   405     return t1;
   406   }
   407   else if( g_isnan(t2->getf()) ) {
   408     return t2;
   409   }
   410   else {
   411     return Type::FLOAT;
   412   }
   413 }
   415 //=============================================================================
   416 //------------------------------Ideal------------------------------------------
   417 Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){
   418   const Type *t2 = phase->type( in(2) );
   419   // Convert "x-c0" into "x+ -c0".
   420   if( t2->base() == Type::DoubleCon ) { // Might be bottom or top...
   421     // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) );
   422   }
   424   // Not associative because of boundary conditions (infinity)
   425   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
   426     // Convert "x - (x+y)" into "-y"
   427     if( in(2)->is_Add() &&
   428         phase->eqv(in(1),in(2)->in(1) ) )
   429       return new (phase->C, 3) SubDNode( phase->makecon(TypeD::ZERO),in(2)->in(2));
   430   }
   432   // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes
   433   // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0.
   434   //if( phase->type(in(1)) == TypeD::ZERO )
   435   //return new (phase->C, 2) NegDNode(in(2));
   437   return NULL;
   438 }
   440 //------------------------------sub--------------------------------------------
   441 // A subtract node differences its two inputs.
   442 const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const {
   443   // no folding if one of operands is infinity or NaN, do not do constant folding
   444   if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) {
   445     return TypeD::make( t1->getd() - t2->getd() );
   446   }
   447   else if( g_isnan(t1->getd()) ) {
   448     return t1;
   449   }
   450   else if( g_isnan(t2->getd()) ) {
   451     return t2;
   452   }
   453   else {
   454     return Type::DOUBLE;
   455   }
   456 }
   458 //=============================================================================
   459 //------------------------------Idealize---------------------------------------
   460 // Unlike SubNodes, compare must still flatten return value to the
   461 // range -1, 0, 1.
   462 // And optimizations like those for (X + Y) - X fail if overflow happens.
   463 Node *CmpNode::Identity( PhaseTransform *phase ) {
   464   return this;
   465 }
   467 //=============================================================================
   468 //------------------------------cmp--------------------------------------------
   469 // Simplify a CmpI (compare 2 integers) node, based on local information.
   470 // If both inputs are constants, compare them.
   471 const Type *CmpINode::sub( const Type *t1, const Type *t2 ) const {
   472   const TypeInt *r0 = t1->is_int(); // Handy access
   473   const TypeInt *r1 = t2->is_int();
   475   if( r0->_hi < r1->_lo )       // Range is always low?
   476     return TypeInt::CC_LT;
   477   else if( r0->_lo > r1->_hi )  // Range is always high?
   478     return TypeInt::CC_GT;
   480   else if( r0->is_con() && r1->is_con() ) { // comparing constants?
   481     assert(r0->get_con() == r1->get_con(), "must be equal");
   482     return TypeInt::CC_EQ;      // Equal results.
   483   } else if( r0->_hi == r1->_lo ) // Range is never high?
   484     return TypeInt::CC_LE;
   485   else if( r0->_lo == r1->_hi ) // Range is never low?
   486     return TypeInt::CC_GE;
   487   return TypeInt::CC;           // else use worst case results
   488 }
   490 // Simplify a CmpU (compare 2 integers) node, based on local information.
   491 // If both inputs are constants, compare them.
   492 const Type *CmpUNode::sub( const Type *t1, const Type *t2 ) const {
   493   assert(!t1->isa_ptr(), "obsolete usage of CmpU");
   495   // comparing two unsigned ints
   496   const TypeInt *r0 = t1->is_int();   // Handy access
   497   const TypeInt *r1 = t2->is_int();
   499   // Current installed version
   500   // Compare ranges for non-overlap
   501   juint lo0 = r0->_lo;
   502   juint hi0 = r0->_hi;
   503   juint lo1 = r1->_lo;
   504   juint hi1 = r1->_hi;
   506   // If either one has both negative and positive values,
   507   // it therefore contains both 0 and -1, and since [0..-1] is the
   508   // full unsigned range, the type must act as an unsigned bottom.
   509   bool bot0 = ((jint)(lo0 ^ hi0) < 0);
   510   bool bot1 = ((jint)(lo1 ^ hi1) < 0);
   512   if (bot0 || bot1) {
   513     // All unsigned values are LE -1 and GE 0.
   514     if (lo0 == 0 && hi0 == 0) {
   515       return TypeInt::CC_LE;            //   0 <= bot
   516     } else if (lo1 == 0 && hi1 == 0) {
   517       return TypeInt::CC_GE;            // bot >= 0
   518     }
   519   } else {
   520     // We can use ranges of the form [lo..hi] if signs are the same.
   521     assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
   522     // results are reversed, '-' > '+' for unsigned compare
   523     if (hi0 < lo1) {
   524       return TypeInt::CC_LT;            // smaller
   525     } else if (lo0 > hi1) {
   526       return TypeInt::CC_GT;            // greater
   527     } else if (hi0 == lo1 && lo0 == hi1) {
   528       return TypeInt::CC_EQ;            // Equal results
   529     } else if (lo0 >= hi1) {
   530       return TypeInt::CC_GE;
   531     } else if (hi0 <= lo1) {
   532       // Check for special case in Hashtable::get.  (See below.)
   533       if ((jint)lo0 >= 0 && (jint)lo1 >= 0 &&
   534           in(1)->Opcode() == Op_ModI &&
   535           in(1)->in(2) == in(2) )
   536         return TypeInt::CC_LT;
   537       return TypeInt::CC_LE;
   538     }
   539   }
   540   // Check for special case in Hashtable::get - the hash index is
   541   // mod'ed to the table size so the following range check is useless.
   542   // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have
   543   // to be positive.
   544   // (This is a gross hack, since the sub method never
   545   // looks at the structure of the node in any other case.)
   546   if ((jint)lo0 >= 0 && (jint)lo1 >= 0 &&
   547       in(1)->Opcode() == Op_ModI &&
   548       in(1)->in(2)->uncast() == in(2)->uncast())
   549     return TypeInt::CC_LT;
   550   return TypeInt::CC;                   // else use worst case results
   551 }
   553 //------------------------------Idealize---------------------------------------
   554 Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
   555   if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
   556     switch (in(1)->Opcode()) {
   557     case Op_CmpL3:              // Collapse a CmpL3/CmpI into a CmpL
   558       return new (phase->C, 3) CmpLNode(in(1)->in(1),in(1)->in(2));
   559     case Op_CmpF3:              // Collapse a CmpF3/CmpI into a CmpF
   560       return new (phase->C, 3) CmpFNode(in(1)->in(1),in(1)->in(2));
   561     case Op_CmpD3:              // Collapse a CmpD3/CmpI into a CmpD
   562       return new (phase->C, 3) CmpDNode(in(1)->in(1),in(1)->in(2));
   563     //case Op_SubI:
   564       // If (x - y) cannot overflow, then ((x - y) <?> 0)
   565       // can be turned into (x <?> y).
   566       // This is handled (with more general cases) by Ideal_sub_algebra.
   567     }
   568   }
   569   return NULL;                  // No change
   570 }
   573 //=============================================================================
   574 // Simplify a CmpL (compare 2 longs ) node, based on local information.
   575 // If both inputs are constants, compare them.
   576 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
   577   const TypeLong *r0 = t1->is_long(); // Handy access
   578   const TypeLong *r1 = t2->is_long();
   580   if( r0->_hi < r1->_lo )       // Range is always low?
   581     return TypeInt::CC_LT;
   582   else if( r0->_lo > r1->_hi )  // Range is always high?
   583     return TypeInt::CC_GT;
   585   else if( r0->is_con() && r1->is_con() ) { // comparing constants?
   586     assert(r0->get_con() == r1->get_con(), "must be equal");
   587     return TypeInt::CC_EQ;      // Equal results.
   588   } else if( r0->_hi == r1->_lo ) // Range is never high?
   589     return TypeInt::CC_LE;
   590   else if( r0->_lo == r1->_hi ) // Range is never low?
   591     return TypeInt::CC_GE;
   592   return TypeInt::CC;           // else use worst case results
   593 }
   595 //=============================================================================
   596 //------------------------------sub--------------------------------------------
   597 // Simplify an CmpP (compare 2 pointers) node, based on local information.
   598 // If both inputs are constants, compare them.
   599 const Type *CmpPNode::sub( const Type *t1, const Type *t2 ) const {
   600   const TypePtr *r0 = t1->is_ptr(); // Handy access
   601   const TypePtr *r1 = t2->is_ptr();
   603   // Undefined inputs makes for an undefined result
   604   if( TypePtr::above_centerline(r0->_ptr) ||
   605       TypePtr::above_centerline(r1->_ptr) )
   606     return Type::TOP;
   608   if (r0 == r1 && r0->singleton()) {
   609     // Equal pointer constants (klasses, nulls, etc.)
   610     return TypeInt::CC_EQ;
   611   }
   613   // See if it is 2 unrelated classes.
   614   const TypeOopPtr* p0 = r0->isa_oopptr();
   615   const TypeOopPtr* p1 = r1->isa_oopptr();
   616   if (p0 && p1) {
   617     ciKlass* klass0 = p0->klass();
   618     bool    xklass0 = p0->klass_is_exact();
   619     ciKlass* klass1 = p1->klass();
   620     bool    xklass1 = p1->klass_is_exact();
   621     int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
   622     if (klass0 && klass1 &&
   623         kps != 1 &&             // both or neither are klass pointers
   624         !klass0->is_interface() && // do not trust interfaces
   625         !klass1->is_interface()) {
   626       // See if neither subclasses the other, or if the class on top
   627       // is precise.  In either of these cases, the compare must fail.
   628       if (klass0->equals(klass1)   ||   // if types are unequal but klasses are
   629           !klass0->is_java_klass() ||   // types not part of Java language?
   630           !klass1->is_java_klass()) {   // types not part of Java language?
   631         // Do nothing; we know nothing for imprecise types
   632       } else if (klass0->is_subtype_of(klass1)) {
   633         // If klass1's type is PRECISE, then we can fail.
   634         if (xklass1)  return TypeInt::CC_GT;
   635       } else if (klass1->is_subtype_of(klass0)) {
   636         // If klass0's type is PRECISE, then we can fail.
   637         if (xklass0)  return TypeInt::CC_GT;
   638       } else {                  // Neither subtypes the other
   639         return TypeInt::CC_GT;  // ...so always fail
   640       }
   641     }
   642   }
   644   // Known constants can be compared exactly
   645   // Null can be distinguished from any NotNull pointers
   646   // Unknown inputs makes an unknown result
   647   if( r0->singleton() ) {
   648     intptr_t bits0 = r0->get_con();
   649     if( r1->singleton() )
   650       return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
   651     return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
   652   } else if( r1->singleton() ) {
   653     intptr_t bits1 = r1->get_con();
   654     return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
   655   } else
   656     return TypeInt::CC;
   657 }
   659 //------------------------------Ideal------------------------------------------
   660 // Check for the case of comparing an unknown klass loaded from the primary
   661 // super-type array vs a known klass with no subtypes.  This amounts to
   662 // checking to see an unknown klass subtypes a known klass with no subtypes;
   663 // this only happens on an exact match.  We can shorten this test by 1 load.
   664 Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
   665   // Constant pointer on right?
   666   const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
   667   if (t2 == NULL || !t2->klass_is_exact())
   668     return NULL;
   669   // Get the constant klass we are comparing to.
   670   ciKlass* superklass = t2->klass();
   672   // Now check for LoadKlass on left.
   673   Node* ldk1 = in(1);
   674   if (ldk1->Opcode() != Op_LoadKlass)
   675     return NULL;
   676   // Take apart the address of the LoadKlass:
   677   Node* adr1 = ldk1->in(MemNode::Address);
   678   intptr_t con2 = 0;
   679   Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2);
   680   if (ldk2 == NULL)
   681     return NULL;
   682   if (con2 == oopDesc::klass_offset_in_bytes()) {
   683     // We are inspecting an object's concrete class.
   684     // Short-circuit the check if the query is abstract.
   685     if (superklass->is_interface() ||
   686         superklass->is_abstract()) {
   687       // Make it come out always false:
   688       this->set_req(2, phase->makecon(TypePtr::NULL_PTR));
   689       return this;
   690     }
   691   }
   693   // Check for a LoadKlass from primary supertype array.
   694   // Any nested loadklass from loadklass+con must be from the p.s. array.
   695   if (ldk2->Opcode() != Op_LoadKlass)
   696     return NULL;
   698   // Verify that we understand the situation
   699   if (con2 != (intptr_t) superklass->super_check_offset())
   700     return NULL;                // Might be element-klass loading from array klass
   702   // If 'superklass' has no subklasses and is not an interface, then we are
   703   // assured that the only input which will pass the type check is
   704   // 'superklass' itself.
   705   //
   706   // We could be more liberal here, and allow the optimization on interfaces
   707   // which have a single implementor.  This would require us to increase the
   708   // expressiveness of the add_dependency() mechanism.
   709   // %%% Do this after we fix TypeOopPtr:  Deps are expressive enough now.
   711   // Object arrays must have their base element have no subtypes
   712   while (superklass->is_obj_array_klass()) {
   713     ciType* elem = superklass->as_obj_array_klass()->element_type();
   714     superklass = elem->as_klass();
   715   }
   716   if (superklass->is_instance_klass()) {
   717     ciInstanceKlass* ik = superklass->as_instance_klass();
   718     if (ik->has_subklass() || ik->is_interface())  return NULL;
   719     // Add a dependency if there is a chance that a subclass will be added later.
   720     if (!ik->is_final()) {
   721       phase->C->dependencies()->assert_leaf_type(ik);
   722     }
   723   }
   725   // Bypass the dependent load, and compare directly
   726   this->set_req(1,ldk2);
   728   return this;
   729 }
   731 //=============================================================================
   732 //------------------------------Value------------------------------------------
   733 // Simplify an CmpF (compare 2 floats ) node, based on local information.
   734 // If both inputs are constants, compare them.
   735 const Type *CmpFNode::Value( PhaseTransform *phase ) const {
   736   const Node* in1 = in(1);
   737   const Node* in2 = in(2);
   738   // Either input is TOP ==> the result is TOP
   739   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
   740   if( t1 == Type::TOP ) return Type::TOP;
   741   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
   742   if( t2 == Type::TOP ) return Type::TOP;
   744   // Not constants?  Don't know squat - even if they are the same
   745   // value!  If they are NaN's they compare to LT instead of EQ.
   746   const TypeF *tf1 = t1->isa_float_constant();
   747   const TypeF *tf2 = t2->isa_float_constant();
   748   if( !tf1 || !tf2 ) return TypeInt::CC;
   750   // This implements the Java bytecode fcmpl, so unordered returns -1.
   751   if( tf1->is_nan() || tf2->is_nan() )
   752     return TypeInt::CC_LT;
   754   if( tf1->_f < tf2->_f ) return TypeInt::CC_LT;
   755   if( tf1->_f > tf2->_f ) return TypeInt::CC_GT;
   756   assert( tf1->_f == tf2->_f, "do not understand FP behavior" );
   757   return TypeInt::CC_EQ;
   758 }
   761 //=============================================================================
   762 //------------------------------Value------------------------------------------
   763 // Simplify an CmpD (compare 2 doubles ) node, based on local information.
   764 // If both inputs are constants, compare them.
   765 const Type *CmpDNode::Value( PhaseTransform *phase ) const {
   766   const Node* in1 = in(1);
   767   const Node* in2 = in(2);
   768   // Either input is TOP ==> the result is TOP
   769   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
   770   if( t1 == Type::TOP ) return Type::TOP;
   771   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
   772   if( t2 == Type::TOP ) return Type::TOP;
   774   // Not constants?  Don't know squat - even if they are the same
   775   // value!  If they are NaN's they compare to LT instead of EQ.
   776   const TypeD *td1 = t1->isa_double_constant();
   777   const TypeD *td2 = t2->isa_double_constant();
   778   if( !td1 || !td2 ) return TypeInt::CC;
   780   // This implements the Java bytecode dcmpl, so unordered returns -1.
   781   if( td1->is_nan() || td2->is_nan() )
   782     return TypeInt::CC_LT;
   784   if( td1->_d < td2->_d ) return TypeInt::CC_LT;
   785   if( td1->_d > td2->_d ) return TypeInt::CC_GT;
   786   assert( td1->_d == td2->_d, "do not understand FP behavior" );
   787   return TypeInt::CC_EQ;
   788 }
   790 //------------------------------Ideal------------------------------------------
   791 Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){
   792   // Check if we can change this to a CmpF and remove a ConvD2F operation.
   793   // Change  (CMPD (F2D (float)) (ConD value))
   794   // To      (CMPF      (float)  (ConF value))
   795   // Valid when 'value' does not lose precision as a float.
   796   // Benefits: eliminates conversion, does not require 24-bit mode
   798   // NaNs prevent commuting operands.  This transform works regardless of the
   799   // order of ConD and ConvF2D inputs by preserving the original order.
   800   int idx_f2d = 1;              // ConvF2D on left side?
   801   if( in(idx_f2d)->Opcode() != Op_ConvF2D )
   802     idx_f2d = 2;                // No, swap to check for reversed args
   803   int idx_con = 3-idx_f2d;      // Check for the constant on other input
   805   if( ConvertCmpD2CmpF &&
   806       in(idx_f2d)->Opcode() == Op_ConvF2D &&
   807       in(idx_con)->Opcode() == Op_ConD ) {
   808     const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
   809     double t2_value_as_double = t2->_d;
   810     float  t2_value_as_float  = (float)t2_value_as_double;
   811     if( t2_value_as_double == (double)t2_value_as_float ) {
   812       // Test value can be represented as a float
   813       // Eliminate the conversion to double and create new comparison
   814       Node *new_in1 = in(idx_f2d)->in(1);
   815       Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
   816       if( idx_f2d != 1 ) {      // Must flip args to match original order
   817         Node *tmp = new_in1;
   818         new_in1 = new_in2;
   819         new_in2 = tmp;
   820       }
   821       CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
   822         ? new (phase->C, 3) CmpF3Node( new_in1, new_in2 )
   823         : new (phase->C, 3) CmpFNode ( new_in1, new_in2 ) ;
   824       return new_cmp;           // Changed to CmpFNode
   825     }
   826     // Testing value required the precision of a double
   827   }
   828   return NULL;                  // No change
   829 }
   832 //=============================================================================
   833 //------------------------------cc2logical-------------------------------------
   834 // Convert a condition code type to a logical type
   835 const Type *BoolTest::cc2logical( const Type *CC ) const {
   836   if( CC == Type::TOP ) return Type::TOP;
   837   if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
   838   const TypeInt *ti = CC->is_int();
   839   if( ti->is_con() ) {          // Only 1 kind of condition codes set?
   840     // Match low order 2 bits
   841     int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
   842     if( _test & 4 ) tmp = 1-tmp;     // Optionally complement result
   843     return TypeInt::make(tmp);       // Boolean result
   844   }
   846   if( CC == TypeInt::CC_GE ) {
   847     if( _test == ge ) return TypeInt::ONE;
   848     if( _test == lt ) return TypeInt::ZERO;
   849   }
   850   if( CC == TypeInt::CC_LE ) {
   851     if( _test == le ) return TypeInt::ONE;
   852     if( _test == gt ) return TypeInt::ZERO;
   853   }
   855   return TypeInt::BOOL;
   856 }
   858 //------------------------------dump_spec-------------------------------------
   859 // Print special per-node info
   860 #ifndef PRODUCT
   861 void BoolTest::dump_on(outputStream *st) const {
   862   const char *msg[] = {"eq","gt","??","lt","ne","le","??","ge"};
   863   st->print(msg[_test]);
   864 }
   865 #endif
   867 //=============================================================================
   868 uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); }
   869 uint BoolNode::size_of() const { return sizeof(BoolNode); }
   871 //------------------------------operator==-------------------------------------
   872 uint BoolNode::cmp( const Node &n ) const {
   873   const BoolNode *b = (const BoolNode *)&n; // Cast up
   874   return (_test._test == b->_test._test);
   875 }
   877 //------------------------------clone_cmp--------------------------------------
   878 // Clone a compare/bool tree
   879 static Node *clone_cmp( Node *cmp, Node *cmp1, Node *cmp2, PhaseGVN *gvn, BoolTest::mask test ) {
   880   Node *ncmp = cmp->clone();
   881   ncmp->set_req(1,cmp1);
   882   ncmp->set_req(2,cmp2);
   883   ncmp = gvn->transform( ncmp );
   884   return new (gvn->C, 2) BoolNode( ncmp, test );
   885 }
   887 //-------------------------------make_predicate--------------------------------
   888 Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
   889   if (test_value->is_Con())   return test_value;
   890   if (test_value->is_Bool())  return test_value;
   891   Compile* C = phase->C;
   892   if (test_value->is_CMove() &&
   893       test_value->in(CMoveNode::Condition)->is_Bool()) {
   894     BoolNode*   bol   = test_value->in(CMoveNode::Condition)->as_Bool();
   895     const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
   896     const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
   897     if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
   898       return bol;
   899     } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
   900       return phase->transform( bol->negate(phase) );
   901     }
   902     // Else fall through.  The CMove gets in the way of the test.
   903     // It should be the case that make_predicate(bol->as_int_value()) == bol.
   904   }
   905   Node* cmp = new (C, 3) CmpINode(test_value, phase->intcon(0));
   906   cmp = phase->transform(cmp);
   907   Node* bol = new (C, 2) BoolNode(cmp, BoolTest::ne);
   908   return phase->transform(bol);
   909 }
   911 //--------------------------------as_int_value---------------------------------
   912 Node* BoolNode::as_int_value(PhaseGVN* phase) {
   913   // Inverse to make_predicate.  The CMove probably boils down to a Conv2B.
   914   Node* cmov = CMoveNode::make(phase->C, NULL, this,
   915                                phase->intcon(0), phase->intcon(1),
   916                                TypeInt::BOOL);
   917   return phase->transform(cmov);
   918 }
   920 //----------------------------------negate-------------------------------------
   921 BoolNode* BoolNode::negate(PhaseGVN* phase) {
   922   Compile* C = phase->C;
   923   return new (C, 2) BoolNode(in(1), _test.negate());
   924 }
   927 //------------------------------Ideal------------------------------------------
   928 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
   929   // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
   930   // This moves the constant to the right.  Helps value-numbering.
   931   Node *cmp = in(1);
   932   if( !cmp->is_Sub() ) return NULL;
   933   int cop = cmp->Opcode();
   934   if( cop == Op_FastLock || cop == Op_FastUnlock ) return NULL;
   935   Node *cmp1 = cmp->in(1);
   936   Node *cmp2 = cmp->in(2);
   937   if( !cmp1 ) return NULL;
   939   // Constant on left?
   940   Node *con = cmp1;
   941   uint op2 = cmp2->Opcode();
   942   // Move constants to the right of compare's to canonicalize.
   943   // Do not muck with Opaque1 nodes, as this indicates a loop
   944   // guard that cannot change shape.
   945   if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 &&
   946       // Because of NaN's, CmpD and CmpF are not commutative
   947       cop != Op_CmpD && cop != Op_CmpF &&
   948       // Protect against swapping inputs to a compare when it is used by a
   949       // counted loop exit, which requires maintaining the loop-limit as in(2)
   950       !is_counted_loop_exit_test() ) {
   951     // Ok, commute the constant to the right of the cmp node.
   952     // Clone the Node, getting a new Node of the same class
   953     cmp = cmp->clone();
   954     // Swap inputs to the clone
   955     cmp->swap_edges(1, 2);
   956     cmp = phase->transform( cmp );
   957     return new (phase->C, 2) BoolNode( cmp, _test.commute() );
   958   }
   960   // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
   961   // The XOR-1 is an idiom used to flip the sense of a bool.  We flip the
   962   // test instead.
   963   int cmp1_op = cmp1->Opcode();
   964   const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
   965   if (cmp2_type == NULL)  return NULL;
   966   Node* j_xor = cmp1;
   967   if( cmp2_type == TypeInt::ZERO &&
   968       cmp1_op == Op_XorI &&
   969       j_xor->in(1) != j_xor &&          // An xor of itself is dead
   970       phase->type( j_xor->in(2) ) == TypeInt::ONE &&
   971       (_test._test == BoolTest::eq ||
   972        _test._test == BoolTest::ne) ) {
   973     Node *ncmp = phase->transform(new (phase->C, 3) CmpINode(j_xor->in(1),cmp2));
   974     return new (phase->C, 2) BoolNode( ncmp, _test.negate() );
   975   }
   977   // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
   978   // This is a standard idiom for branching on a boolean value.
   979   Node *c2b = cmp1;
   980   if( cmp2_type == TypeInt::ZERO &&
   981       cmp1_op == Op_Conv2B &&
   982       (_test._test == BoolTest::eq ||
   983        _test._test == BoolTest::ne) ) {
   984     Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
   985        ? (Node*)new (phase->C, 3) CmpINode(c2b->in(1),cmp2)
   986        : (Node*)new (phase->C, 3) CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
   987     );
   988     return new (phase->C, 2) BoolNode( ncmp, _test._test );
   989   }
   991   // Comparing a SubI against a zero is equal to comparing the SubI
   992   // arguments directly.  This only works for eq and ne comparisons
   993   // due to possible integer overflow.
   994   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
   995         (cop == Op_CmpI) &&
   996         (cmp1->Opcode() == Op_SubI) &&
   997         ( cmp2_type == TypeInt::ZERO ) ) {
   998     Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(1),cmp1->in(2)));
   999     return new (phase->C, 2) BoolNode( ncmp, _test._test );
  1002   // Change (-A vs 0) into (A vs 0) by commuting the test.  Disallow in the
  1003   // most general case because negating 0x80000000 does nothing.  Needed for
  1004   // the CmpF3/SubI/CmpI idiom.
  1005   if( cop == Op_CmpI &&
  1006       cmp1->Opcode() == Op_SubI &&
  1007       cmp2_type == TypeInt::ZERO &&
  1008       phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
  1009       phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
  1010     Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(2),cmp2));
  1011     return new (phase->C, 2) BoolNode( ncmp, _test.commute() );
  1014   //  The transformation below is not valid for either signed or unsigned
  1015   //  comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
  1016   //  This transformation can be resurrected when we are able to
  1017   //  make inferences about the range of values being subtracted from
  1018   //  (or added to) relative to the wraparound point.
  1019   //
  1020   //    // Remove +/-1's if possible.
  1021   //    // "X <= Y-1" becomes "X <  Y"
  1022   //    // "X+1 <= Y" becomes "X <  Y"
  1023   //    // "X <  Y+1" becomes "X <= Y"
  1024   //    // "X-1 <  Y" becomes "X <= Y"
  1025   //    // Do not this to compares off of the counted-loop-end.  These guys are
  1026   //    // checking the trip counter and they want to use the post-incremented
  1027   //    // counter.  If they use the PRE-incremented counter, then the counter has
  1028   //    // to be incremented in a private block on a loop backedge.
  1029   //    if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
  1030   //      return NULL;
  1031   //  #ifndef PRODUCT
  1032   //    // Do not do this in a wash GVN pass during verification.
  1033   //    // Gets triggered by too many simple optimizations to be bothered with
  1034   //    // re-trying it again and again.
  1035   //    if( !phase->allow_progress() ) return NULL;
  1036   //  #endif
  1037   //    // Not valid for unsigned compare because of corner cases in involving zero.
  1038   //    // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an
  1039   //    // exception in case X is 0 (because 0-1 turns into 4billion unsigned but
  1040   //    // "0 <=u Y" is always true).
  1041   //    if( cmp->Opcode() == Op_CmpU ) return NULL;
  1042   //    int cmp2_op = cmp2->Opcode();
  1043   //    if( _test._test == BoolTest::le ) {
  1044   //      if( cmp1_op == Op_AddI &&
  1045   //          phase->type( cmp1->in(2) ) == TypeInt::ONE )
  1046   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt );
  1047   //      else if( cmp2_op == Op_AddI &&
  1048   //         phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 )
  1049   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt );
  1050   //    } else if( _test._test == BoolTest::lt ) {
  1051   //      if( cmp1_op == Op_AddI &&
  1052   //          phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 )
  1053   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le );
  1054   //      else if( cmp2_op == Op_AddI &&
  1055   //         phase->type( cmp2->in(2) ) == TypeInt::ONE )
  1056   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le );
  1057   //    }
  1059   return NULL;
  1062 //------------------------------Value------------------------------------------
  1063 // Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
  1064 // based on local information.   If the input is constant, do it.
  1065 const Type *BoolNode::Value( PhaseTransform *phase ) const {
  1066   return _test.cc2logical( phase->type( in(1) ) );
  1069 //------------------------------dump_spec--------------------------------------
  1070 // Dump special per-node info
  1071 #ifndef PRODUCT
  1072 void BoolNode::dump_spec(outputStream *st) const {
  1073   st->print("[");
  1074   _test.dump_on(st);
  1075   st->print("]");
  1077 #endif
  1079 //------------------------------is_counted_loop_exit_test--------------------------------------
  1080 // Returns true if node is used by a counted loop node.
  1081 bool BoolNode::is_counted_loop_exit_test() {
  1082   for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
  1083     Node* use = fast_out(i);
  1084     if (use->is_CountedLoopEnd()) {
  1085       return true;
  1088   return false;
  1091 //=============================================================================
  1092 //------------------------------NegNode----------------------------------------
  1093 Node *NegFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  1094   if( in(1)->Opcode() == Op_SubF )
  1095     return new (phase->C, 3) SubFNode( in(1)->in(2), in(1)->in(1) );
  1096   return NULL;
  1099 Node *NegDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  1100   if( in(1)->Opcode() == Op_SubD )
  1101     return new (phase->C, 3) SubDNode( in(1)->in(2), in(1)->in(1) );
  1102   return NULL;
  1106 //=============================================================================
  1107 //------------------------------Value------------------------------------------
  1108 // Compute sqrt
  1109 const Type *SqrtDNode::Value( PhaseTransform *phase ) const {
  1110   const Type *t1 = phase->type( in(1) );
  1111   if( t1 == Type::TOP ) return Type::TOP;
  1112   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1113   double d = t1->getd();
  1114   if( d < 0.0 ) return Type::DOUBLE;
  1115   return TypeD::make( sqrt( d ) );
  1118 //=============================================================================
  1119 //------------------------------Value------------------------------------------
  1120 // Compute cos
  1121 const Type *CosDNode::Value( PhaseTransform *phase ) const {
  1122   const Type *t1 = phase->type( in(1) );
  1123   if( t1 == Type::TOP ) return Type::TOP;
  1124   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1125   double d = t1->getd();
  1126   if( d < 0.0 ) return Type::DOUBLE;
  1127   return TypeD::make( SharedRuntime::dcos( d ) );
  1130 //=============================================================================
  1131 //------------------------------Value------------------------------------------
  1132 // Compute sin
  1133 const Type *SinDNode::Value( PhaseTransform *phase ) const {
  1134   const Type *t1 = phase->type( in(1) );
  1135   if( t1 == Type::TOP ) return Type::TOP;
  1136   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1137   double d = t1->getd();
  1138   if( d < 0.0 ) return Type::DOUBLE;
  1139   return TypeD::make( SharedRuntime::dsin( d ) );
  1142 //=============================================================================
  1143 //------------------------------Value------------------------------------------
  1144 // Compute tan
  1145 const Type *TanDNode::Value( PhaseTransform *phase ) const {
  1146   const Type *t1 = phase->type( in(1) );
  1147   if( t1 == Type::TOP ) return Type::TOP;
  1148   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1149   double d = t1->getd();
  1150   if( d < 0.0 ) return Type::DOUBLE;
  1151   return TypeD::make( SharedRuntime::dtan( d ) );
  1154 //=============================================================================
  1155 //------------------------------Value------------------------------------------
  1156 // Compute log
  1157 const Type *LogDNode::Value( PhaseTransform *phase ) const {
  1158   const Type *t1 = phase->type( in(1) );
  1159   if( t1 == Type::TOP ) return Type::TOP;
  1160   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1161   double d = t1->getd();
  1162   if( d < 0.0 ) return Type::DOUBLE;
  1163   return TypeD::make( SharedRuntime::dlog( d ) );
  1166 //=============================================================================
  1167 //------------------------------Value------------------------------------------
  1168 // Compute log10
  1169 const Type *Log10DNode::Value( PhaseTransform *phase ) const {
  1170   const Type *t1 = phase->type( in(1) );
  1171   if( t1 == Type::TOP ) return Type::TOP;
  1172   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1173   double d = t1->getd();
  1174   if( d < 0.0 ) return Type::DOUBLE;
  1175   return TypeD::make( SharedRuntime::dlog10( d ) );
  1178 //=============================================================================
  1179 //------------------------------Value------------------------------------------
  1180 // Compute exp
  1181 const Type *ExpDNode::Value( PhaseTransform *phase ) const {
  1182   const Type *t1 = phase->type( in(1) );
  1183   if( t1 == Type::TOP ) return Type::TOP;
  1184   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1185   double d = t1->getd();
  1186   if( d < 0.0 ) return Type::DOUBLE;
  1187   return TypeD::make( SharedRuntime::dexp( d ) );
  1191 //=============================================================================
  1192 //------------------------------Value------------------------------------------
  1193 // Compute pow
  1194 const Type *PowDNode::Value( PhaseTransform *phase ) const {
  1195   const Type *t1 = phase->type( in(1) );
  1196   if( t1 == Type::TOP ) return Type::TOP;
  1197   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1198   const Type *t2 = phase->type( in(2) );
  1199   if( t2 == Type::TOP ) return Type::TOP;
  1200   if( t2->base() != Type::DoubleCon ) return Type::DOUBLE;
  1201   double d1 = t1->getd();
  1202   double d2 = t2->getd();
  1203   if( d1 < 0.0 ) return Type::DOUBLE;
  1204   if( d2 < 0.0 ) return Type::DOUBLE;
  1205   return TypeD::make( SharedRuntime::dpow( d1, d2 ) );

mercurial