src/share/vm/opto/subnode.cpp

changeset 435
a61af66fc99e
child 468
3288958bf319
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/opto/subnode.cpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,1206 @@
     1.4 +/*
     1.5 + * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +// Portions of code courtesy of Clifford Click
    1.29 +
    1.30 +// Optimization - Graph Style
    1.31 +
    1.32 +#include "incls/_precompiled.incl"
    1.33 +#include "incls/_subnode.cpp.incl"
    1.34 +#include "math.h"
    1.35 +
    1.36 +//=============================================================================
    1.37 +//------------------------------Identity---------------------------------------
    1.38 +// If right input is a constant 0, return the left input.
    1.39 +Node *SubNode::Identity( PhaseTransform *phase ) {
    1.40 +  assert(in(1) != this, "Must already have called Value");
    1.41 +  assert(in(2) != this, "Must already have called Value");
    1.42 +
    1.43 +  // Remove double negation
    1.44 +  const Type *zero = add_id();
    1.45 +  if( phase->type( in(1) )->higher_equal( zero ) &&
    1.46 +      in(2)->Opcode() == Opcode() &&
    1.47 +      phase->type( in(2)->in(1) )->higher_equal( zero ) ) {
    1.48 +    return in(2)->in(2);
    1.49 +  }
    1.50 +
    1.51 +  // Convert "(X+Y) - Y" into X
    1.52 +  if( in(1)->Opcode() == Op_AddI ) {
    1.53 +    if( phase->eqv(in(1)->in(2),in(2)) )
    1.54 +      return in(1)->in(1);
    1.55 +    // Also catch: "(X + Opaque2(Y)) - Y".  In this case, 'Y' is a loop-varying
    1.56 +    // trip counter and X is likely to be loop-invariant (that's how O2 Nodes
    1.57 +    // are originally used, although the optimizer sometimes jiggers things).
    1.58 +    // This folding through an O2 removes a loop-exit use of a loop-varying
    1.59 +    // value and generally lowers register pressure in and around the loop.
    1.60 +    if( in(1)->in(2)->Opcode() == Op_Opaque2 &&
    1.61 +        phase->eqv(in(1)->in(2)->in(1),in(2)) )
    1.62 +      return in(1)->in(1);
    1.63 +  }
    1.64 +
    1.65 +  return ( phase->type( in(2) )->higher_equal( zero ) ) ? in(1) : this;
    1.66 +}
    1.67 +
    1.68 +//------------------------------Value------------------------------------------
    1.69 +// A subtract node differences it's two inputs.
    1.70 +const Type *SubNode::Value( PhaseTransform *phase ) const {
    1.71 +  const Node* in1 = in(1);
    1.72 +  const Node* in2 = in(2);
    1.73 +  // Either input is TOP ==> the result is TOP
    1.74 +  const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
    1.75 +  if( t1 == Type::TOP ) return Type::TOP;
    1.76 +  const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
    1.77 +  if( t2 == Type::TOP ) return Type::TOP;
    1.78 +
    1.79 +  // Not correct for SubFnode and AddFNode (must check for infinity)
    1.80 +  // Equal?  Subtract is zero
    1.81 +  if (phase->eqv_uncast(in1, in2))  return add_id();
    1.82 +
    1.83 +  // Either input is BOTTOM ==> the result is the local BOTTOM
    1.84 +  if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
    1.85 +    return bottom_type();
    1.86 +
    1.87 +  return sub(t1,t2);            // Local flavor of type subtraction
    1.88 +
    1.89 +}
    1.90 +
    1.91 +//=============================================================================
    1.92 +
    1.93 +//------------------------------Helper function--------------------------------
    1.94 +static bool ok_to_convert(Node* inc, Node* iv) {
    1.95 +    // Do not collapse (x+c0)-y if "+" is a loop increment, because the
    1.96 +    // "-" is loop invariant and collapsing extends the live-range of "x"
    1.97 +    // to overlap with the "+", forcing another register to be used in
    1.98 +    // the loop.
    1.99 +    // This test will be clearer with '&&' (apply DeMorgan's rule)
   1.100 +    // but I like the early cutouts that happen here.
   1.101 +    const PhiNode *phi;
   1.102 +    if( ( !inc->in(1)->is_Phi() ||
   1.103 +          !(phi=inc->in(1)->as_Phi()) ||
   1.104 +          phi->is_copy() ||
   1.105 +          !phi->region()->is_CountedLoop() ||
   1.106 +          inc != phi->region()->as_CountedLoop()->incr() )
   1.107 +       &&
   1.108 +        // Do not collapse (x+c0)-iv if "iv" is a loop induction variable,
   1.109 +        // because "x" maybe invariant.
   1.110 +        ( !iv->is_loop_iv() )
   1.111 +      ) {
   1.112 +      return true;
   1.113 +    } else {
   1.114 +      return false;
   1.115 +    }
   1.116 +}
   1.117 +//------------------------------Ideal------------------------------------------
   1.118 +Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){
   1.119 +  Node *in1 = in(1);
   1.120 +  Node *in2 = in(2);
   1.121 +  uint op1 = in1->Opcode();
   1.122 +  uint op2 = in2->Opcode();
   1.123 +
   1.124 +#ifdef ASSERT
   1.125 +  // Check for dead loop
   1.126 +  if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
   1.127 +      ( op1 == Op_AddI || op1 == Op_SubI ) &&
   1.128 +      ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
   1.129 +        phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1 ) ) )
   1.130 +    assert(false, "dead loop in SubINode::Ideal");
   1.131 +#endif
   1.132 +
   1.133 +  const Type *t2 = phase->type( in2 );
   1.134 +  if( t2 == Type::TOP ) return NULL;
   1.135 +  // Convert "x-c0" into "x+ -c0".
   1.136 +  if( t2->base() == Type::Int ){        // Might be bottom or top...
   1.137 +    const TypeInt *i = t2->is_int();
   1.138 +    if( i->is_con() )
   1.139 +      return new (phase->C, 3) AddINode(in1, phase->intcon(-i->get_con()));
   1.140 +  }
   1.141 +
   1.142 +  // Convert "(x+c0) - y" into (x-y) + c0"
   1.143 +  // Do not collapse (x+c0)-y if "+" is a loop increment or
   1.144 +  // if "y" is a loop induction variable.
   1.145 +  if( op1 == Op_AddI && ok_to_convert(in1, in2) ) {
   1.146 +    const Type *tadd = phase->type( in1->in(2) );
   1.147 +    if( tadd->singleton() && tadd != Type::TOP ) {
   1.148 +      Node *sub2 = phase->transform( new (phase->C, 3) SubINode( in1->in(1), in2 ));
   1.149 +      return new (phase->C, 3) AddINode( sub2, in1->in(2) );
   1.150 +    }
   1.151 +  }
   1.152 +
   1.153 +
   1.154 +  // Convert "x - (y+c0)" into "(x-y) - c0"
   1.155 +  // Need the same check as in above optimization but reversed.
   1.156 +  if (op2 == Op_AddI && ok_to_convert(in2, in1)) {
   1.157 +    Node* in21 = in2->in(1);
   1.158 +    Node* in22 = in2->in(2);
   1.159 +    const TypeInt* tcon = phase->type(in22)->isa_int();
   1.160 +    if (tcon != NULL && tcon->is_con()) {
   1.161 +      Node* sub2 = phase->transform( new (phase->C, 3) SubINode(in1, in21) );
   1.162 +      Node* neg_c0 = phase->intcon(- tcon->get_con());
   1.163 +      return new (phase->C, 3) AddINode(sub2, neg_c0);
   1.164 +    }
   1.165 +  }
   1.166 +
   1.167 +  const Type *t1 = phase->type( in1 );
   1.168 +  if( t1 == Type::TOP ) return NULL;
   1.169 +
   1.170 +#ifdef ASSERT
   1.171 +  // Check for dead loop
   1.172 +  if( ( op2 == Op_AddI || op2 == Op_SubI ) &&
   1.173 +      ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
   1.174 +        phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
   1.175 +    assert(false, "dead loop in SubINode::Ideal");
   1.176 +#endif
   1.177 +
   1.178 +  // Convert "x - (x+y)" into "-y"
   1.179 +  if( op2 == Op_AddI &&
   1.180 +      phase->eqv( in1, in2->in(1) ) )
   1.181 +    return new (phase->C, 3) SubINode( phase->intcon(0),in2->in(2));
   1.182 +  // Convert "(x-y) - x" into "-y"
   1.183 +  if( op1 == Op_SubI &&
   1.184 +      phase->eqv( in1->in(1), in2 ) )
   1.185 +    return new (phase->C, 3) SubINode( phase->intcon(0),in1->in(2));
   1.186 +  // Convert "x - (y+x)" into "-y"
   1.187 +  if( op2 == Op_AddI &&
   1.188 +      phase->eqv( in1, in2->in(2) ) )
   1.189 +    return new (phase->C, 3) SubINode( phase->intcon(0),in2->in(1));
   1.190 +
   1.191 +  // Convert "0 - (x-y)" into "y-x"
   1.192 +  if( t1 == TypeInt::ZERO && op2 == Op_SubI )
   1.193 +    return new (phase->C, 3) SubINode( in2->in(2), in2->in(1) );
   1.194 +
   1.195 +  // Convert "0 - (x+con)" into "-con-x"
   1.196 +  jint con;
   1.197 +  if( t1 == TypeInt::ZERO && op2 == Op_AddI &&
   1.198 +      (con = in2->in(2)->find_int_con(0)) != 0 )
   1.199 +    return new (phase->C, 3) SubINode( phase->intcon(-con), in2->in(1) );
   1.200 +
   1.201 +  // Convert "(X+A) - (X+B)" into "A - B"
   1.202 +  if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) )
   1.203 +    return new (phase->C, 3) SubINode( in1->in(2), in2->in(2) );
   1.204 +
   1.205 +  // Convert "(A+X) - (B+X)" into "A - B"
   1.206 +  if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) )
   1.207 +    return new (phase->C, 3) SubINode( in1->in(1), in2->in(1) );
   1.208 +
   1.209 +  // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally
   1.210 +  // nicer to optimize than subtract.
   1.211 +  if( op2 == Op_SubI && in2->outcnt() == 1) {
   1.212 +    Node *add1 = phase->transform( new (phase->C, 3) AddINode( in1, in2->in(2) ) );
   1.213 +    return new (phase->C, 3) SubINode( add1, in2->in(1) );
   1.214 +  }
   1.215 +
   1.216 +  return NULL;
   1.217 +}
   1.218 +
   1.219 +//------------------------------sub--------------------------------------------
   1.220 +// A subtract node differences it's two inputs.
   1.221 +const Type *SubINode::sub( const Type *t1, const Type *t2 ) const {
   1.222 +  const TypeInt *r0 = t1->is_int(); // Handy access
   1.223 +  const TypeInt *r1 = t2->is_int();
   1.224 +  int32 lo = r0->_lo - r1->_hi;
   1.225 +  int32 hi = r0->_hi - r1->_lo;
   1.226 +
   1.227 +  // We next check for 32-bit overflow.
   1.228 +  // If that happens, we just assume all integers are possible.
   1.229 +  if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
   1.230 +       ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
   1.231 +      (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
   1.232 +       ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
   1.233 +    return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen));
   1.234 +  else                          // Overflow; assume all integers
   1.235 +    return TypeInt::INT;
   1.236 +}
   1.237 +
   1.238 +//=============================================================================
   1.239 +//------------------------------Ideal------------------------------------------
   1.240 +Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
   1.241 +  Node *in1 = in(1);
   1.242 +  Node *in2 = in(2);
   1.243 +  uint op1 = in1->Opcode();
   1.244 +  uint op2 = in2->Opcode();
   1.245 +
   1.246 +#ifdef ASSERT
   1.247 +  // Check for dead loop
   1.248 +  if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
   1.249 +      ( op1 == Op_AddL || op1 == Op_SubL ) &&
   1.250 +      ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
   1.251 +        phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1  ) ) )
   1.252 +    assert(false, "dead loop in SubLNode::Ideal");
   1.253 +#endif
   1.254 +
   1.255 +  if( phase->type( in2 ) == Type::TOP ) return NULL;
   1.256 +  const TypeLong *i = phase->type( in2 )->isa_long();
   1.257 +  // Convert "x-c0" into "x+ -c0".
   1.258 +  if( i &&                      // Might be bottom or top...
   1.259 +      i->is_con() )
   1.260 +    return new (phase->C, 3) AddLNode(in1, phase->longcon(-i->get_con()));
   1.261 +
   1.262 +  // Convert "(x+c0) - y" into (x-y) + c0"
   1.263 +  // Do not collapse (x+c0)-y if "+" is a loop increment or
   1.264 +  // if "y" is a loop induction variable.
   1.265 +  if( op1 == Op_AddL && ok_to_convert(in1, in2) ) {
   1.266 +    Node *in11 = in1->in(1);
   1.267 +    const Type *tadd = phase->type( in1->in(2) );
   1.268 +    if( tadd->singleton() && tadd != Type::TOP ) {
   1.269 +      Node *sub2 = phase->transform( new (phase->C, 3) SubLNode( in11, in2 ));
   1.270 +      return new (phase->C, 3) AddLNode( sub2, in1->in(2) );
   1.271 +    }
   1.272 +  }
   1.273 +
   1.274 +  // Convert "x - (y+c0)" into "(x-y) - c0"
   1.275 +  // Need the same check as in above optimization but reversed.
   1.276 +  if (op2 == Op_AddL && ok_to_convert(in2, in1)) {
   1.277 +    Node* in21 = in2->in(1);
   1.278 +    Node* in22 = in2->in(2);
   1.279 +    const TypeLong* tcon = phase->type(in22)->isa_long();
   1.280 +    if (tcon != NULL && tcon->is_con()) {
   1.281 +      Node* sub2 = phase->transform( new (phase->C, 3) SubLNode(in1, in21) );
   1.282 +      Node* neg_c0 = phase->longcon(- tcon->get_con());
   1.283 +      return new (phase->C, 3) AddLNode(sub2, neg_c0);
   1.284 +    }
   1.285 +  }
   1.286 +
   1.287 +  const Type *t1 = phase->type( in1 );
   1.288 +  if( t1 == Type::TOP ) return NULL;
   1.289 +
   1.290 +#ifdef ASSERT
   1.291 +  // Check for dead loop
   1.292 +  if( ( op2 == Op_AddL || op2 == Op_SubL ) &&
   1.293 +      ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
   1.294 +        phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
   1.295 +    assert(false, "dead loop in SubLNode::Ideal");
   1.296 +#endif
   1.297 +
   1.298 +  // Convert "x - (x+y)" into "-y"
   1.299 +  if( op2 == Op_AddL &&
   1.300 +      phase->eqv( in1, in2->in(1) ) )
   1.301 +    return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO), in2->in(2));
   1.302 +  // Convert "x - (y+x)" into "-y"
   1.303 +  if( op2 == Op_AddL &&
   1.304 +      phase->eqv( in1, in2->in(2) ) )
   1.305 +    return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO),in2->in(1));
   1.306 +
   1.307 +  // Convert "0 - (x-y)" into "y-x"
   1.308 +  if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL )
   1.309 +    return new (phase->C, 3) SubLNode( in2->in(2), in2->in(1) );
   1.310 +
   1.311 +  // Convert "(X+A) - (X+B)" into "A - B"
   1.312 +  if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) )
   1.313 +    return new (phase->C, 3) SubLNode( in1->in(2), in2->in(2) );
   1.314 +
   1.315 +  // Convert "(A+X) - (B+X)" into "A - B"
   1.316 +  if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) )
   1.317 +    return new (phase->C, 3) SubLNode( in1->in(1), in2->in(1) );
   1.318 +
   1.319 +  // Convert "A-(B-C)" into (A+C)-B"
   1.320 +  if( op2 == Op_SubL && in2->outcnt() == 1) {
   1.321 +    Node *add1 = phase->transform( new (phase->C, 3) AddLNode( in1, in2->in(2) ) );
   1.322 +    return new (phase->C, 3) SubLNode( add1, in2->in(1) );
   1.323 +  }
   1.324 +
   1.325 +  return NULL;
   1.326 +}
   1.327 +
   1.328 +//------------------------------sub--------------------------------------------
   1.329 +// A subtract node differences it's two inputs.
   1.330 +const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const {
   1.331 +  const TypeLong *r0 = t1->is_long(); // Handy access
   1.332 +  const TypeLong *r1 = t2->is_long();
   1.333 +  jlong lo = r0->_lo - r1->_hi;
   1.334 +  jlong hi = r0->_hi - r1->_lo;
   1.335 +
   1.336 +  // We next check for 32-bit overflow.
   1.337 +  // If that happens, we just assume all integers are possible.
   1.338 +  if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
   1.339 +       ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
   1.340 +      (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
   1.341 +       ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
   1.342 +    return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen));
   1.343 +  else                          // Overflow; assume all integers
   1.344 +    return TypeLong::LONG;
   1.345 +}
   1.346 +
   1.347 +//=============================================================================
   1.348 +//------------------------------Value------------------------------------------
   1.349 +// A subtract node differences its two inputs.
   1.350 +const Type *SubFPNode::Value( PhaseTransform *phase ) const {
   1.351 +  const Node* in1 = in(1);
   1.352 +  const Node* in2 = in(2);
   1.353 +  // Either input is TOP ==> the result is TOP
   1.354 +  const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
   1.355 +  if( t1 == Type::TOP ) return Type::TOP;
   1.356 +  const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
   1.357 +  if( t2 == Type::TOP ) return Type::TOP;
   1.358 +
   1.359 +  // if both operands are infinity of same sign, the result is NaN; do
   1.360 +  // not replace with zero
   1.361 +  if( (t1->is_finite() && t2->is_finite()) ) {
   1.362 +    if( phase->eqv(in1, in2) ) return add_id();
   1.363 +  }
   1.364 +
   1.365 +  // Either input is BOTTOM ==> the result is the local BOTTOM
   1.366 +  const Type *bot = bottom_type();
   1.367 +  if( (t1 == bot) || (t2 == bot) ||
   1.368 +      (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
   1.369 +    return bot;
   1.370 +
   1.371 +  return sub(t1,t2);            // Local flavor of type subtraction
   1.372 +}
   1.373 +
   1.374 +
   1.375 +//=============================================================================
   1.376 +//------------------------------Ideal------------------------------------------
   1.377 +Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
   1.378 +  const Type *t2 = phase->type( in(2) );
   1.379 +  // Convert "x-c0" into "x+ -c0".
   1.380 +  if( t2->base() == Type::FloatCon ) {  // Might be bottom or top...
   1.381 +    // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) );
   1.382 +  }
   1.383 +
   1.384 +  // Not associative because of boundary conditions (infinity)
   1.385 +  if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
   1.386 +    // Convert "x - (x+y)" into "-y"
   1.387 +    if( in(2)->is_Add() &&
   1.388 +        phase->eqv(in(1),in(2)->in(1) ) )
   1.389 +      return new (phase->C, 3) SubFNode( phase->makecon(TypeF::ZERO),in(2)->in(2));
   1.390 +  }
   1.391 +
   1.392 +  // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes
   1.393 +  // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0.
   1.394 +  //if( phase->type(in(1)) == TypeF::ZERO )
   1.395 +  //return new (phase->C, 2) NegFNode(in(2));
   1.396 +
   1.397 +  return NULL;
   1.398 +}
   1.399 +
   1.400 +//------------------------------sub--------------------------------------------
   1.401 +// A subtract node differences its two inputs.
   1.402 +const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const {
   1.403 +  // no folding if one of operands is infinity or NaN, do not do constant folding
   1.404 +  if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) {
   1.405 +    return TypeF::make( t1->getf() - t2->getf() );
   1.406 +  }
   1.407 +  else if( g_isnan(t1->getf()) ) {
   1.408 +    return t1;
   1.409 +  }
   1.410 +  else if( g_isnan(t2->getf()) ) {
   1.411 +    return t2;
   1.412 +  }
   1.413 +  else {
   1.414 +    return Type::FLOAT;
   1.415 +  }
   1.416 +}
   1.417 +
   1.418 +//=============================================================================
   1.419 +//------------------------------Ideal------------------------------------------
   1.420 +Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){
   1.421 +  const Type *t2 = phase->type( in(2) );
   1.422 +  // Convert "x-c0" into "x+ -c0".
   1.423 +  if( t2->base() == Type::DoubleCon ) { // Might be bottom or top...
   1.424 +    // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) );
   1.425 +  }
   1.426 +
   1.427 +  // Not associative because of boundary conditions (infinity)
   1.428 +  if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
   1.429 +    // Convert "x - (x+y)" into "-y"
   1.430 +    if( in(2)->is_Add() &&
   1.431 +        phase->eqv(in(1),in(2)->in(1) ) )
   1.432 +      return new (phase->C, 3) SubDNode( phase->makecon(TypeD::ZERO),in(2)->in(2));
   1.433 +  }
   1.434 +
   1.435 +  // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes
   1.436 +  // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0.
   1.437 +  //if( phase->type(in(1)) == TypeD::ZERO )
   1.438 +  //return new (phase->C, 2) NegDNode(in(2));
   1.439 +
   1.440 +  return NULL;
   1.441 +}
   1.442 +
   1.443 +//------------------------------sub--------------------------------------------
   1.444 +// A subtract node differences its two inputs.
   1.445 +const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const {
   1.446 +  // no folding if one of operands is infinity or NaN, do not do constant folding
   1.447 +  if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) {
   1.448 +    return TypeD::make( t1->getd() - t2->getd() );
   1.449 +  }
   1.450 +  else if( g_isnan(t1->getd()) ) {
   1.451 +    return t1;
   1.452 +  }
   1.453 +  else if( g_isnan(t2->getd()) ) {
   1.454 +    return t2;
   1.455 +  }
   1.456 +  else {
   1.457 +    return Type::DOUBLE;
   1.458 +  }
   1.459 +}
   1.460 +
   1.461 +//=============================================================================
   1.462 +//------------------------------Idealize---------------------------------------
   1.463 +// Unlike SubNodes, compare must still flatten return value to the
   1.464 +// range -1, 0, 1.
   1.465 +// And optimizations like those for (X + Y) - X fail if overflow happens.
   1.466 +Node *CmpNode::Identity( PhaseTransform *phase ) {
   1.467 +  return this;
   1.468 +}
   1.469 +
   1.470 +//=============================================================================
   1.471 +//------------------------------cmp--------------------------------------------
   1.472 +// Simplify a CmpI (compare 2 integers) node, based on local information.
   1.473 +// If both inputs are constants, compare them.
   1.474 +const Type *CmpINode::sub( const Type *t1, const Type *t2 ) const {
   1.475 +  const TypeInt *r0 = t1->is_int(); // Handy access
   1.476 +  const TypeInt *r1 = t2->is_int();
   1.477 +
   1.478 +  if( r0->_hi < r1->_lo )       // Range is always low?
   1.479 +    return TypeInt::CC_LT;
   1.480 +  else if( r0->_lo > r1->_hi )  // Range is always high?
   1.481 +    return TypeInt::CC_GT;
   1.482 +
   1.483 +  else if( r0->is_con() && r1->is_con() ) { // comparing constants?
   1.484 +    assert(r0->get_con() == r1->get_con(), "must be equal");
   1.485 +    return TypeInt::CC_EQ;      // Equal results.
   1.486 +  } else if( r0->_hi == r1->_lo ) // Range is never high?
   1.487 +    return TypeInt::CC_LE;
   1.488 +  else if( r0->_lo == r1->_hi ) // Range is never low?
   1.489 +    return TypeInt::CC_GE;
   1.490 +  return TypeInt::CC;           // else use worst case results
   1.491 +}
   1.492 +
   1.493 +// Simplify a CmpU (compare 2 integers) node, based on local information.
   1.494 +// If both inputs are constants, compare them.
   1.495 +const Type *CmpUNode::sub( const Type *t1, const Type *t2 ) const {
   1.496 +  assert(!t1->isa_ptr(), "obsolete usage of CmpU");
   1.497 +
   1.498 +  // comparing two unsigned ints
   1.499 +  const TypeInt *r0 = t1->is_int();   // Handy access
   1.500 +  const TypeInt *r1 = t2->is_int();
   1.501 +
   1.502 +  // Current installed version
   1.503 +  // Compare ranges for non-overlap
   1.504 +  juint lo0 = r0->_lo;
   1.505 +  juint hi0 = r0->_hi;
   1.506 +  juint lo1 = r1->_lo;
   1.507 +  juint hi1 = r1->_hi;
   1.508 +
   1.509 +  // If either one has both negative and positive values,
   1.510 +  // it therefore contains both 0 and -1, and since [0..-1] is the
   1.511 +  // full unsigned range, the type must act as an unsigned bottom.
   1.512 +  bool bot0 = ((jint)(lo0 ^ hi0) < 0);
   1.513 +  bool bot1 = ((jint)(lo1 ^ hi1) < 0);
   1.514 +
   1.515 +  if (bot0 || bot1) {
   1.516 +    // All unsigned values are LE -1 and GE 0.
   1.517 +    if (lo0 == 0 && hi0 == 0) {
   1.518 +      return TypeInt::CC_LE;            //   0 <= bot
   1.519 +    } else if (lo1 == 0 && hi1 == 0) {
   1.520 +      return TypeInt::CC_GE;            // bot >= 0
   1.521 +    }
   1.522 +  } else {
   1.523 +    // We can use ranges of the form [lo..hi] if signs are the same.
   1.524 +    assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
   1.525 +    // results are reversed, '-' > '+' for unsigned compare
   1.526 +    if (hi0 < lo1) {
   1.527 +      return TypeInt::CC_LT;            // smaller
   1.528 +    } else if (lo0 > hi1) {
   1.529 +      return TypeInt::CC_GT;            // greater
   1.530 +    } else if (hi0 == lo1 && lo0 == hi1) {
   1.531 +      return TypeInt::CC_EQ;            // Equal results
   1.532 +    } else if (lo0 >= hi1) {
   1.533 +      return TypeInt::CC_GE;
   1.534 +    } else if (hi0 <= lo1) {
   1.535 +      // Check for special case in Hashtable::get.  (See below.)
   1.536 +      if ((jint)lo0 >= 0 && (jint)lo1 >= 0 &&
   1.537 +          in(1)->Opcode() == Op_ModI &&
   1.538 +          in(1)->in(2) == in(2) )
   1.539 +        return TypeInt::CC_LT;
   1.540 +      return TypeInt::CC_LE;
   1.541 +    }
   1.542 +  }
   1.543 +  // Check for special case in Hashtable::get - the hash index is
   1.544 +  // mod'ed to the table size so the following range check is useless.
   1.545 +  // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have
   1.546 +  // to be positive.
   1.547 +  // (This is a gross hack, since the sub method never
   1.548 +  // looks at the structure of the node in any other case.)
   1.549 +  if ((jint)lo0 >= 0 && (jint)lo1 >= 0 &&
   1.550 +      in(1)->Opcode() == Op_ModI &&
   1.551 +      in(1)->in(2)->uncast() == in(2)->uncast())
   1.552 +    return TypeInt::CC_LT;
   1.553 +  return TypeInt::CC;                   // else use worst case results
   1.554 +}
   1.555 +
   1.556 +//------------------------------Idealize---------------------------------------
   1.557 +Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
   1.558 +  if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
   1.559 +    switch (in(1)->Opcode()) {
   1.560 +    case Op_CmpL3:              // Collapse a CmpL3/CmpI into a CmpL
   1.561 +      return new (phase->C, 3) CmpLNode(in(1)->in(1),in(1)->in(2));
   1.562 +    case Op_CmpF3:              // Collapse a CmpF3/CmpI into a CmpF
   1.563 +      return new (phase->C, 3) CmpFNode(in(1)->in(1),in(1)->in(2));
   1.564 +    case Op_CmpD3:              // Collapse a CmpD3/CmpI into a CmpD
   1.565 +      return new (phase->C, 3) CmpDNode(in(1)->in(1),in(1)->in(2));
   1.566 +    //case Op_SubI:
   1.567 +      // If (x - y) cannot overflow, then ((x - y) <?> 0)
   1.568 +      // can be turned into (x <?> y).
   1.569 +      // This is handled (with more general cases) by Ideal_sub_algebra.
   1.570 +    }
   1.571 +  }
   1.572 +  return NULL;                  // No change
   1.573 +}
   1.574 +
   1.575 +
   1.576 +//=============================================================================
   1.577 +// Simplify a CmpL (compare 2 longs ) node, based on local information.
   1.578 +// If both inputs are constants, compare them.
   1.579 +const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
   1.580 +  const TypeLong *r0 = t1->is_long(); // Handy access
   1.581 +  const TypeLong *r1 = t2->is_long();
   1.582 +
   1.583 +  if( r0->_hi < r1->_lo )       // Range is always low?
   1.584 +    return TypeInt::CC_LT;
   1.585 +  else if( r0->_lo > r1->_hi )  // Range is always high?
   1.586 +    return TypeInt::CC_GT;
   1.587 +
   1.588 +  else if( r0->is_con() && r1->is_con() ) { // comparing constants?
   1.589 +    assert(r0->get_con() == r1->get_con(), "must be equal");
   1.590 +    return TypeInt::CC_EQ;      // Equal results.
   1.591 +  } else if( r0->_hi == r1->_lo ) // Range is never high?
   1.592 +    return TypeInt::CC_LE;
   1.593 +  else if( r0->_lo == r1->_hi ) // Range is never low?
   1.594 +    return TypeInt::CC_GE;
   1.595 +  return TypeInt::CC;           // else use worst case results
   1.596 +}
   1.597 +
   1.598 +//=============================================================================
   1.599 +//------------------------------sub--------------------------------------------
   1.600 +// Simplify an CmpP (compare 2 pointers) node, based on local information.
   1.601 +// If both inputs are constants, compare them.
   1.602 +const Type *CmpPNode::sub( const Type *t1, const Type *t2 ) const {
   1.603 +  const TypePtr *r0 = t1->is_ptr(); // Handy access
   1.604 +  const TypePtr *r1 = t2->is_ptr();
   1.605 +
   1.606 +  // Undefined inputs makes for an undefined result
   1.607 +  if( TypePtr::above_centerline(r0->_ptr) ||
   1.608 +      TypePtr::above_centerline(r1->_ptr) )
   1.609 +    return Type::TOP;
   1.610 +
   1.611 +  if (r0 == r1 && r0->singleton()) {
   1.612 +    // Equal pointer constants (klasses, nulls, etc.)
   1.613 +    return TypeInt::CC_EQ;
   1.614 +  }
   1.615 +
   1.616 +  // See if it is 2 unrelated classes.
   1.617 +  const TypeOopPtr* p0 = r0->isa_oopptr();
   1.618 +  const TypeOopPtr* p1 = r1->isa_oopptr();
   1.619 +  if (p0 && p1) {
   1.620 +    ciKlass* klass0 = p0->klass();
   1.621 +    bool    xklass0 = p0->klass_is_exact();
   1.622 +    ciKlass* klass1 = p1->klass();
   1.623 +    bool    xklass1 = p1->klass_is_exact();
   1.624 +    int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
   1.625 +    if (klass0 && klass1 &&
   1.626 +        kps != 1 &&             // both or neither are klass pointers
   1.627 +        !klass0->is_interface() && // do not trust interfaces
   1.628 +        !klass1->is_interface()) {
   1.629 +      // See if neither subclasses the other, or if the class on top
   1.630 +      // is precise.  In either of these cases, the compare must fail.
   1.631 +      if (klass0->equals(klass1)   ||   // if types are unequal but klasses are
   1.632 +          !klass0->is_java_klass() ||   // types not part of Java language?
   1.633 +          !klass1->is_java_klass()) {   // types not part of Java language?
   1.634 +        // Do nothing; we know nothing for imprecise types
   1.635 +      } else if (klass0->is_subtype_of(klass1)) {
   1.636 +        // If klass1's type is PRECISE, then we can fail.
   1.637 +        if (xklass1)  return TypeInt::CC_GT;
   1.638 +      } else if (klass1->is_subtype_of(klass0)) {
   1.639 +        // If klass0's type is PRECISE, then we can fail.
   1.640 +        if (xklass0)  return TypeInt::CC_GT;
   1.641 +      } else {                  // Neither subtypes the other
   1.642 +        return TypeInt::CC_GT;  // ...so always fail
   1.643 +      }
   1.644 +    }
   1.645 +  }
   1.646 +
   1.647 +  // Known constants can be compared exactly
   1.648 +  // Null can be distinguished from any NotNull pointers
   1.649 +  // Unknown inputs makes an unknown result
   1.650 +  if( r0->singleton() ) {
   1.651 +    intptr_t bits0 = r0->get_con();
   1.652 +    if( r1->singleton() )
   1.653 +      return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
   1.654 +    return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
   1.655 +  } else if( r1->singleton() ) {
   1.656 +    intptr_t bits1 = r1->get_con();
   1.657 +    return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
   1.658 +  } else
   1.659 +    return TypeInt::CC;
   1.660 +}
   1.661 +
   1.662 +//------------------------------Ideal------------------------------------------
   1.663 +// Check for the case of comparing an unknown klass loaded from the primary
   1.664 +// super-type array vs a known klass with no subtypes.  This amounts to
   1.665 +// checking to see an unknown klass subtypes a known klass with no subtypes;
   1.666 +// this only happens on an exact match.  We can shorten this test by 1 load.
   1.667 +Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
   1.668 +  // Constant pointer on right?
   1.669 +  const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
   1.670 +  if (t2 == NULL || !t2->klass_is_exact())
   1.671 +    return NULL;
   1.672 +  // Get the constant klass we are comparing to.
   1.673 +  ciKlass* superklass = t2->klass();
   1.674 +
   1.675 +  // Now check for LoadKlass on left.
   1.676 +  Node* ldk1 = in(1);
   1.677 +  if (ldk1->Opcode() != Op_LoadKlass)
   1.678 +    return NULL;
   1.679 +  // Take apart the address of the LoadKlass:
   1.680 +  Node* adr1 = ldk1->in(MemNode::Address);
   1.681 +  intptr_t con2 = 0;
   1.682 +  Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2);
   1.683 +  if (ldk2 == NULL)
   1.684 +    return NULL;
   1.685 +  if (con2 == oopDesc::klass_offset_in_bytes()) {
   1.686 +    // We are inspecting an object's concrete class.
   1.687 +    // Short-circuit the check if the query is abstract.
   1.688 +    if (superklass->is_interface() ||
   1.689 +        superklass->is_abstract()) {
   1.690 +      // Make it come out always false:
   1.691 +      this->set_req(2, phase->makecon(TypePtr::NULL_PTR));
   1.692 +      return this;
   1.693 +    }
   1.694 +  }
   1.695 +
   1.696 +  // Check for a LoadKlass from primary supertype array.
   1.697 +  // Any nested loadklass from loadklass+con must be from the p.s. array.
   1.698 +  if (ldk2->Opcode() != Op_LoadKlass)
   1.699 +    return NULL;
   1.700 +
   1.701 +  // Verify that we understand the situation
   1.702 +  if (con2 != (intptr_t) superklass->super_check_offset())
   1.703 +    return NULL;                // Might be element-klass loading from array klass
   1.704 +
   1.705 +  // If 'superklass' has no subklasses and is not an interface, then we are
   1.706 +  // assured that the only input which will pass the type check is
   1.707 +  // 'superklass' itself.
   1.708 +  //
   1.709 +  // We could be more liberal here, and allow the optimization on interfaces
   1.710 +  // which have a single implementor.  This would require us to increase the
   1.711 +  // expressiveness of the add_dependency() mechanism.
   1.712 +  // %%% Do this after we fix TypeOopPtr:  Deps are expressive enough now.
   1.713 +
   1.714 +  // Object arrays must have their base element have no subtypes
   1.715 +  while (superklass->is_obj_array_klass()) {
   1.716 +    ciType* elem = superklass->as_obj_array_klass()->element_type();
   1.717 +    superklass = elem->as_klass();
   1.718 +  }
   1.719 +  if (superklass->is_instance_klass()) {
   1.720 +    ciInstanceKlass* ik = superklass->as_instance_klass();
   1.721 +    if (ik->has_subklass() || ik->is_interface())  return NULL;
   1.722 +    // Add a dependency if there is a chance that a subclass will be added later.
   1.723 +    if (!ik->is_final()) {
   1.724 +      phase->C->dependencies()->assert_leaf_type(ik);
   1.725 +    }
   1.726 +  }
   1.727 +
   1.728 +  // Bypass the dependent load, and compare directly
   1.729 +  this->set_req(1,ldk2);
   1.730 +
   1.731 +  return this;
   1.732 +}
   1.733 +
   1.734 +//=============================================================================
   1.735 +//------------------------------Value------------------------------------------
   1.736 +// Simplify an CmpF (compare 2 floats ) node, based on local information.
   1.737 +// If both inputs are constants, compare them.
   1.738 +const Type *CmpFNode::Value( PhaseTransform *phase ) const {
   1.739 +  const Node* in1 = in(1);
   1.740 +  const Node* in2 = in(2);
   1.741 +  // Either input is TOP ==> the result is TOP
   1.742 +  const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
   1.743 +  if( t1 == Type::TOP ) return Type::TOP;
   1.744 +  const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
   1.745 +  if( t2 == Type::TOP ) return Type::TOP;
   1.746 +
   1.747 +  // Not constants?  Don't know squat - even if they are the same
   1.748 +  // value!  If they are NaN's they compare to LT instead of EQ.
   1.749 +  const TypeF *tf1 = t1->isa_float_constant();
   1.750 +  const TypeF *tf2 = t2->isa_float_constant();
   1.751 +  if( !tf1 || !tf2 ) return TypeInt::CC;
   1.752 +
   1.753 +  // This implements the Java bytecode fcmpl, so unordered returns -1.
   1.754 +  if( tf1->is_nan() || tf2->is_nan() )
   1.755 +    return TypeInt::CC_LT;
   1.756 +
   1.757 +  if( tf1->_f < tf2->_f ) return TypeInt::CC_LT;
   1.758 +  if( tf1->_f > tf2->_f ) return TypeInt::CC_GT;
   1.759 +  assert( tf1->_f == tf2->_f, "do not understand FP behavior" );
   1.760 +  return TypeInt::CC_EQ;
   1.761 +}
   1.762 +
   1.763 +
   1.764 +//=============================================================================
   1.765 +//------------------------------Value------------------------------------------
   1.766 +// Simplify an CmpD (compare 2 doubles ) node, based on local information.
   1.767 +// If both inputs are constants, compare them.
   1.768 +const Type *CmpDNode::Value( PhaseTransform *phase ) const {
   1.769 +  const Node* in1 = in(1);
   1.770 +  const Node* in2 = in(2);
   1.771 +  // Either input is TOP ==> the result is TOP
   1.772 +  const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
   1.773 +  if( t1 == Type::TOP ) return Type::TOP;
   1.774 +  const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
   1.775 +  if( t2 == Type::TOP ) return Type::TOP;
   1.776 +
   1.777 +  // Not constants?  Don't know squat - even if they are the same
   1.778 +  // value!  If they are NaN's they compare to LT instead of EQ.
   1.779 +  const TypeD *td1 = t1->isa_double_constant();
   1.780 +  const TypeD *td2 = t2->isa_double_constant();
   1.781 +  if( !td1 || !td2 ) return TypeInt::CC;
   1.782 +
   1.783 +  // This implements the Java bytecode dcmpl, so unordered returns -1.
   1.784 +  if( td1->is_nan() || td2->is_nan() )
   1.785 +    return TypeInt::CC_LT;
   1.786 +
   1.787 +  if( td1->_d < td2->_d ) return TypeInt::CC_LT;
   1.788 +  if( td1->_d > td2->_d ) return TypeInt::CC_GT;
   1.789 +  assert( td1->_d == td2->_d, "do not understand FP behavior" );
   1.790 +  return TypeInt::CC_EQ;
   1.791 +}
   1.792 +
   1.793 +//------------------------------Ideal------------------------------------------
   1.794 +Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){
   1.795 +  // Check if we can change this to a CmpF and remove a ConvD2F operation.
   1.796 +  // Change  (CMPD (F2D (float)) (ConD value))
   1.797 +  // To      (CMPF      (float)  (ConF value))
   1.798 +  // Valid when 'value' does not lose precision as a float.
   1.799 +  // Benefits: eliminates conversion, does not require 24-bit mode
   1.800 +
   1.801 +  // NaNs prevent commuting operands.  This transform works regardless of the
   1.802 +  // order of ConD and ConvF2D inputs by preserving the original order.
   1.803 +  int idx_f2d = 1;              // ConvF2D on left side?
   1.804 +  if( in(idx_f2d)->Opcode() != Op_ConvF2D )
   1.805 +    idx_f2d = 2;                // No, swap to check for reversed args
   1.806 +  int idx_con = 3-idx_f2d;      // Check for the constant on other input
   1.807 +
   1.808 +  if( ConvertCmpD2CmpF &&
   1.809 +      in(idx_f2d)->Opcode() == Op_ConvF2D &&
   1.810 +      in(idx_con)->Opcode() == Op_ConD ) {
   1.811 +    const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
   1.812 +    double t2_value_as_double = t2->_d;
   1.813 +    float  t2_value_as_float  = (float)t2_value_as_double;
   1.814 +    if( t2_value_as_double == (double)t2_value_as_float ) {
   1.815 +      // Test value can be represented as a float
   1.816 +      // Eliminate the conversion to double and create new comparison
   1.817 +      Node *new_in1 = in(idx_f2d)->in(1);
   1.818 +      Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
   1.819 +      if( idx_f2d != 1 ) {      // Must flip args to match original order
   1.820 +        Node *tmp = new_in1;
   1.821 +        new_in1 = new_in2;
   1.822 +        new_in2 = tmp;
   1.823 +      }
   1.824 +      CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
   1.825 +        ? new (phase->C, 3) CmpF3Node( new_in1, new_in2 )
   1.826 +        : new (phase->C, 3) CmpFNode ( new_in1, new_in2 ) ;
   1.827 +      return new_cmp;           // Changed to CmpFNode
   1.828 +    }
   1.829 +    // Testing value required the precision of a double
   1.830 +  }
   1.831 +  return NULL;                  // No change
   1.832 +}
   1.833 +
   1.834 +
   1.835 +//=============================================================================
   1.836 +//------------------------------cc2logical-------------------------------------
   1.837 +// Convert a condition code type to a logical type
   1.838 +const Type *BoolTest::cc2logical( const Type *CC ) const {
   1.839 +  if( CC == Type::TOP ) return Type::TOP;
   1.840 +  if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
   1.841 +  const TypeInt *ti = CC->is_int();
   1.842 +  if( ti->is_con() ) {          // Only 1 kind of condition codes set?
   1.843 +    // Match low order 2 bits
   1.844 +    int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
   1.845 +    if( _test & 4 ) tmp = 1-tmp;     // Optionally complement result
   1.846 +    return TypeInt::make(tmp);       // Boolean result
   1.847 +  }
   1.848 +
   1.849 +  if( CC == TypeInt::CC_GE ) {
   1.850 +    if( _test == ge ) return TypeInt::ONE;
   1.851 +    if( _test == lt ) return TypeInt::ZERO;
   1.852 +  }
   1.853 +  if( CC == TypeInt::CC_LE ) {
   1.854 +    if( _test == le ) return TypeInt::ONE;
   1.855 +    if( _test == gt ) return TypeInt::ZERO;
   1.856 +  }
   1.857 +
   1.858 +  return TypeInt::BOOL;
   1.859 +}
   1.860 +
   1.861 +//------------------------------dump_spec-------------------------------------
   1.862 +// Print special per-node info
   1.863 +#ifndef PRODUCT
   1.864 +void BoolTest::dump_on(outputStream *st) const {
   1.865 +  const char *msg[] = {"eq","gt","??","lt","ne","le","??","ge"};
   1.866 +  st->print(msg[_test]);
   1.867 +}
   1.868 +#endif
   1.869 +
   1.870 +//=============================================================================
   1.871 +uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); }
   1.872 +uint BoolNode::size_of() const { return sizeof(BoolNode); }
   1.873 +
   1.874 +//------------------------------operator==-------------------------------------
   1.875 +uint BoolNode::cmp( const Node &n ) const {
   1.876 +  const BoolNode *b = (const BoolNode *)&n; // Cast up
   1.877 +  return (_test._test == b->_test._test);
   1.878 +}
   1.879 +
   1.880 +//------------------------------clone_cmp--------------------------------------
   1.881 +// Clone a compare/bool tree
   1.882 +static Node *clone_cmp( Node *cmp, Node *cmp1, Node *cmp2, PhaseGVN *gvn, BoolTest::mask test ) {
   1.883 +  Node *ncmp = cmp->clone();
   1.884 +  ncmp->set_req(1,cmp1);
   1.885 +  ncmp->set_req(2,cmp2);
   1.886 +  ncmp = gvn->transform( ncmp );
   1.887 +  return new (gvn->C, 2) BoolNode( ncmp, test );
   1.888 +}
   1.889 +
   1.890 +//-------------------------------make_predicate--------------------------------
   1.891 +Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
   1.892 +  if (test_value->is_Con())   return test_value;
   1.893 +  if (test_value->is_Bool())  return test_value;
   1.894 +  Compile* C = phase->C;
   1.895 +  if (test_value->is_CMove() &&
   1.896 +      test_value->in(CMoveNode::Condition)->is_Bool()) {
   1.897 +    BoolNode*   bol   = test_value->in(CMoveNode::Condition)->as_Bool();
   1.898 +    const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
   1.899 +    const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
   1.900 +    if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
   1.901 +      return bol;
   1.902 +    } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
   1.903 +      return phase->transform( bol->negate(phase) );
   1.904 +    }
   1.905 +    // Else fall through.  The CMove gets in the way of the test.
   1.906 +    // It should be the case that make_predicate(bol->as_int_value()) == bol.
   1.907 +  }
   1.908 +  Node* cmp = new (C, 3) CmpINode(test_value, phase->intcon(0));
   1.909 +  cmp = phase->transform(cmp);
   1.910 +  Node* bol = new (C, 2) BoolNode(cmp, BoolTest::ne);
   1.911 +  return phase->transform(bol);
   1.912 +}
   1.913 +
   1.914 +//--------------------------------as_int_value---------------------------------
   1.915 +Node* BoolNode::as_int_value(PhaseGVN* phase) {
   1.916 +  // Inverse to make_predicate.  The CMove probably boils down to a Conv2B.
   1.917 +  Node* cmov = CMoveNode::make(phase->C, NULL, this,
   1.918 +                               phase->intcon(0), phase->intcon(1),
   1.919 +                               TypeInt::BOOL);
   1.920 +  return phase->transform(cmov);
   1.921 +}
   1.922 +
   1.923 +//----------------------------------negate-------------------------------------
   1.924 +BoolNode* BoolNode::negate(PhaseGVN* phase) {
   1.925 +  Compile* C = phase->C;
   1.926 +  return new (C, 2) BoolNode(in(1), _test.negate());
   1.927 +}
   1.928 +
   1.929 +
   1.930 +//------------------------------Ideal------------------------------------------
   1.931 +Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
   1.932 +  // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
   1.933 +  // This moves the constant to the right.  Helps value-numbering.
   1.934 +  Node *cmp = in(1);
   1.935 +  if( !cmp->is_Sub() ) return NULL;
   1.936 +  int cop = cmp->Opcode();
   1.937 +  if( cop == Op_FastLock || cop == Op_FastUnlock ) return NULL;
   1.938 +  Node *cmp1 = cmp->in(1);
   1.939 +  Node *cmp2 = cmp->in(2);
   1.940 +  if( !cmp1 ) return NULL;
   1.941 +
   1.942 +  // Constant on left?
   1.943 +  Node *con = cmp1;
   1.944 +  uint op2 = cmp2->Opcode();
   1.945 +  // Move constants to the right of compare's to canonicalize.
   1.946 +  // Do not muck with Opaque1 nodes, as this indicates a loop
   1.947 +  // guard that cannot change shape.
   1.948 +  if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 &&
   1.949 +      // Because of NaN's, CmpD and CmpF are not commutative
   1.950 +      cop != Op_CmpD && cop != Op_CmpF &&
   1.951 +      // Protect against swapping inputs to a compare when it is used by a
   1.952 +      // counted loop exit, which requires maintaining the loop-limit as in(2)
   1.953 +      !is_counted_loop_exit_test() ) {
   1.954 +    // Ok, commute the constant to the right of the cmp node.
   1.955 +    // Clone the Node, getting a new Node of the same class
   1.956 +    cmp = cmp->clone();
   1.957 +    // Swap inputs to the clone
   1.958 +    cmp->swap_edges(1, 2);
   1.959 +    cmp = phase->transform( cmp );
   1.960 +    return new (phase->C, 2) BoolNode( cmp, _test.commute() );
   1.961 +  }
   1.962 +
   1.963 +  // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
   1.964 +  // The XOR-1 is an idiom used to flip the sense of a bool.  We flip the
   1.965 +  // test instead.
   1.966 +  int cmp1_op = cmp1->Opcode();
   1.967 +  const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
   1.968 +  if (cmp2_type == NULL)  return NULL;
   1.969 +  Node* j_xor = cmp1;
   1.970 +  if( cmp2_type == TypeInt::ZERO &&
   1.971 +      cmp1_op == Op_XorI &&
   1.972 +      j_xor->in(1) != j_xor &&          // An xor of itself is dead
   1.973 +      phase->type( j_xor->in(2) ) == TypeInt::ONE &&
   1.974 +      (_test._test == BoolTest::eq ||
   1.975 +       _test._test == BoolTest::ne) ) {
   1.976 +    Node *ncmp = phase->transform(new (phase->C, 3) CmpINode(j_xor->in(1),cmp2));
   1.977 +    return new (phase->C, 2) BoolNode( ncmp, _test.negate() );
   1.978 +  }
   1.979 +
   1.980 +  // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
   1.981 +  // This is a standard idiom for branching on a boolean value.
   1.982 +  Node *c2b = cmp1;
   1.983 +  if( cmp2_type == TypeInt::ZERO &&
   1.984 +      cmp1_op == Op_Conv2B &&
   1.985 +      (_test._test == BoolTest::eq ||
   1.986 +       _test._test == BoolTest::ne) ) {
   1.987 +    Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
   1.988 +       ? (Node*)new (phase->C, 3) CmpINode(c2b->in(1),cmp2)
   1.989 +       : (Node*)new (phase->C, 3) CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
   1.990 +    );
   1.991 +    return new (phase->C, 2) BoolNode( ncmp, _test._test );
   1.992 +  }
   1.993 +
   1.994 +  // Comparing a SubI against a zero is equal to comparing the SubI
   1.995 +  // arguments directly.  This only works for eq and ne comparisons
   1.996 +  // due to possible integer overflow.
   1.997 +  if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
   1.998 +        (cop == Op_CmpI) &&
   1.999 +        (cmp1->Opcode() == Op_SubI) &&
  1.1000 +        ( cmp2_type == TypeInt::ZERO ) ) {
  1.1001 +    Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(1),cmp1->in(2)));
  1.1002 +    return new (phase->C, 2) BoolNode( ncmp, _test._test );
  1.1003 +  }
  1.1004 +
  1.1005 +  // Change (-A vs 0) into (A vs 0) by commuting the test.  Disallow in the
  1.1006 +  // most general case because negating 0x80000000 does nothing.  Needed for
  1.1007 +  // the CmpF3/SubI/CmpI idiom.
  1.1008 +  if( cop == Op_CmpI &&
  1.1009 +      cmp1->Opcode() == Op_SubI &&
  1.1010 +      cmp2_type == TypeInt::ZERO &&
  1.1011 +      phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
  1.1012 +      phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
  1.1013 +    Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(2),cmp2));
  1.1014 +    return new (phase->C, 2) BoolNode( ncmp, _test.commute() );
  1.1015 +  }
  1.1016 +
  1.1017 +  //  The transformation below is not valid for either signed or unsigned
  1.1018 +  //  comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
  1.1019 +  //  This transformation can be resurrected when we are able to
  1.1020 +  //  make inferences about the range of values being subtracted from
  1.1021 +  //  (or added to) relative to the wraparound point.
  1.1022 +  //
  1.1023 +  //    // Remove +/-1's if possible.
  1.1024 +  //    // "X <= Y-1" becomes "X <  Y"
  1.1025 +  //    // "X+1 <= Y" becomes "X <  Y"
  1.1026 +  //    // "X <  Y+1" becomes "X <= Y"
  1.1027 +  //    // "X-1 <  Y" becomes "X <= Y"
  1.1028 +  //    // Do not this to compares off of the counted-loop-end.  These guys are
  1.1029 +  //    // checking the trip counter and they want to use the post-incremented
  1.1030 +  //    // counter.  If they use the PRE-incremented counter, then the counter has
  1.1031 +  //    // to be incremented in a private block on a loop backedge.
  1.1032 +  //    if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
  1.1033 +  //      return NULL;
  1.1034 +  //  #ifndef PRODUCT
  1.1035 +  //    // Do not do this in a wash GVN pass during verification.
  1.1036 +  //    // Gets triggered by too many simple optimizations to be bothered with
  1.1037 +  //    // re-trying it again and again.
  1.1038 +  //    if( !phase->allow_progress() ) return NULL;
  1.1039 +  //  #endif
  1.1040 +  //    // Not valid for unsigned compare because of corner cases in involving zero.
  1.1041 +  //    // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an
  1.1042 +  //    // exception in case X is 0 (because 0-1 turns into 4billion unsigned but
  1.1043 +  //    // "0 <=u Y" is always true).
  1.1044 +  //    if( cmp->Opcode() == Op_CmpU ) return NULL;
  1.1045 +  //    int cmp2_op = cmp2->Opcode();
  1.1046 +  //    if( _test._test == BoolTest::le ) {
  1.1047 +  //      if( cmp1_op == Op_AddI &&
  1.1048 +  //          phase->type( cmp1->in(2) ) == TypeInt::ONE )
  1.1049 +  //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt );
  1.1050 +  //      else if( cmp2_op == Op_AddI &&
  1.1051 +  //         phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 )
  1.1052 +  //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt );
  1.1053 +  //    } else if( _test._test == BoolTest::lt ) {
  1.1054 +  //      if( cmp1_op == Op_AddI &&
  1.1055 +  //          phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 )
  1.1056 +  //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le );
  1.1057 +  //      else if( cmp2_op == Op_AddI &&
  1.1058 +  //         phase->type( cmp2->in(2) ) == TypeInt::ONE )
  1.1059 +  //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le );
  1.1060 +  //    }
  1.1061 +
  1.1062 +  return NULL;
  1.1063 +}
  1.1064 +
  1.1065 +//------------------------------Value------------------------------------------
  1.1066 +// Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
  1.1067 +// based on local information.   If the input is constant, do it.
  1.1068 +const Type *BoolNode::Value( PhaseTransform *phase ) const {
  1.1069 +  return _test.cc2logical( phase->type( in(1) ) );
  1.1070 +}
  1.1071 +
  1.1072 +//------------------------------dump_spec--------------------------------------
  1.1073 +// Dump special per-node info
  1.1074 +#ifndef PRODUCT
  1.1075 +void BoolNode::dump_spec(outputStream *st) const {
  1.1076 +  st->print("[");
  1.1077 +  _test.dump_on(st);
  1.1078 +  st->print("]");
  1.1079 +}
  1.1080 +#endif
  1.1081 +
  1.1082 +//------------------------------is_counted_loop_exit_test--------------------------------------
  1.1083 +// Returns true if node is used by a counted loop node.
  1.1084 +bool BoolNode::is_counted_loop_exit_test() {
  1.1085 +  for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
  1.1086 +    Node* use = fast_out(i);
  1.1087 +    if (use->is_CountedLoopEnd()) {
  1.1088 +      return true;
  1.1089 +    }
  1.1090 +  }
  1.1091 +  return false;
  1.1092 +}
  1.1093 +
  1.1094 +//=============================================================================
  1.1095 +//------------------------------NegNode----------------------------------------
  1.1096 +Node *NegFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  1.1097 +  if( in(1)->Opcode() == Op_SubF )
  1.1098 +    return new (phase->C, 3) SubFNode( in(1)->in(2), in(1)->in(1) );
  1.1099 +  return NULL;
  1.1100 +}
  1.1101 +
  1.1102 +Node *NegDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  1.1103 +  if( in(1)->Opcode() == Op_SubD )
  1.1104 +    return new (phase->C, 3) SubDNode( in(1)->in(2), in(1)->in(1) );
  1.1105 +  return NULL;
  1.1106 +}
  1.1107 +
  1.1108 +
  1.1109 +//=============================================================================
  1.1110 +//------------------------------Value------------------------------------------
  1.1111 +// Compute sqrt
  1.1112 +const Type *SqrtDNode::Value( PhaseTransform *phase ) const {
  1.1113 +  const Type *t1 = phase->type( in(1) );
  1.1114 +  if( t1 == Type::TOP ) return Type::TOP;
  1.1115 +  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1.1116 +  double d = t1->getd();
  1.1117 +  if( d < 0.0 ) return Type::DOUBLE;
  1.1118 +  return TypeD::make( sqrt( d ) );
  1.1119 +}
  1.1120 +
  1.1121 +//=============================================================================
  1.1122 +//------------------------------Value------------------------------------------
  1.1123 +// Compute cos
  1.1124 +const Type *CosDNode::Value( PhaseTransform *phase ) const {
  1.1125 +  const Type *t1 = phase->type( in(1) );
  1.1126 +  if( t1 == Type::TOP ) return Type::TOP;
  1.1127 +  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1.1128 +  double d = t1->getd();
  1.1129 +  if( d < 0.0 ) return Type::DOUBLE;
  1.1130 +  return TypeD::make( SharedRuntime::dcos( d ) );
  1.1131 +}
  1.1132 +
  1.1133 +//=============================================================================
  1.1134 +//------------------------------Value------------------------------------------
  1.1135 +// Compute sin
  1.1136 +const Type *SinDNode::Value( PhaseTransform *phase ) const {
  1.1137 +  const Type *t1 = phase->type( in(1) );
  1.1138 +  if( t1 == Type::TOP ) return Type::TOP;
  1.1139 +  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1.1140 +  double d = t1->getd();
  1.1141 +  if( d < 0.0 ) return Type::DOUBLE;
  1.1142 +  return TypeD::make( SharedRuntime::dsin( d ) );
  1.1143 +}
  1.1144 +
  1.1145 +//=============================================================================
  1.1146 +//------------------------------Value------------------------------------------
  1.1147 +// Compute tan
  1.1148 +const Type *TanDNode::Value( PhaseTransform *phase ) const {
  1.1149 +  const Type *t1 = phase->type( in(1) );
  1.1150 +  if( t1 == Type::TOP ) return Type::TOP;
  1.1151 +  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1.1152 +  double d = t1->getd();
  1.1153 +  if( d < 0.0 ) return Type::DOUBLE;
  1.1154 +  return TypeD::make( SharedRuntime::dtan( d ) );
  1.1155 +}
  1.1156 +
  1.1157 +//=============================================================================
  1.1158 +//------------------------------Value------------------------------------------
  1.1159 +// Compute log
  1.1160 +const Type *LogDNode::Value( PhaseTransform *phase ) const {
  1.1161 +  const Type *t1 = phase->type( in(1) );
  1.1162 +  if( t1 == Type::TOP ) return Type::TOP;
  1.1163 +  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1.1164 +  double d = t1->getd();
  1.1165 +  if( d < 0.0 ) return Type::DOUBLE;
  1.1166 +  return TypeD::make( SharedRuntime::dlog( d ) );
  1.1167 +}
  1.1168 +
  1.1169 +//=============================================================================
  1.1170 +//------------------------------Value------------------------------------------
  1.1171 +// Compute log10
  1.1172 +const Type *Log10DNode::Value( PhaseTransform *phase ) const {
  1.1173 +  const Type *t1 = phase->type( in(1) );
  1.1174 +  if( t1 == Type::TOP ) return Type::TOP;
  1.1175 +  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1.1176 +  double d = t1->getd();
  1.1177 +  if( d < 0.0 ) return Type::DOUBLE;
  1.1178 +  return TypeD::make( SharedRuntime::dlog10( d ) );
  1.1179 +}
  1.1180 +
  1.1181 +//=============================================================================
  1.1182 +//------------------------------Value------------------------------------------
  1.1183 +// Compute exp
  1.1184 +const Type *ExpDNode::Value( PhaseTransform *phase ) const {
  1.1185 +  const Type *t1 = phase->type( in(1) );
  1.1186 +  if( t1 == Type::TOP ) return Type::TOP;
  1.1187 +  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1.1188 +  double d = t1->getd();
  1.1189 +  if( d < 0.0 ) return Type::DOUBLE;
  1.1190 +  return TypeD::make( SharedRuntime::dexp( d ) );
  1.1191 +}
  1.1192 +
  1.1193 +
  1.1194 +//=============================================================================
  1.1195 +//------------------------------Value------------------------------------------
  1.1196 +// Compute pow
  1.1197 +const Type *PowDNode::Value( PhaseTransform *phase ) const {
  1.1198 +  const Type *t1 = phase->type( in(1) );
  1.1199 +  if( t1 == Type::TOP ) return Type::TOP;
  1.1200 +  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1.1201 +  const Type *t2 = phase->type( in(2) );
  1.1202 +  if( t2 == Type::TOP ) return Type::TOP;
  1.1203 +  if( t2->base() != Type::DoubleCon ) return Type::DOUBLE;
  1.1204 +  double d1 = t1->getd();
  1.1205 +  double d2 = t2->getd();
  1.1206 +  if( d1 < 0.0 ) return Type::DOUBLE;
  1.1207 +  if( d2 < 0.0 ) return Type::DOUBLE;
  1.1208 +  return TypeD::make( SharedRuntime::dpow( d1, d2 ) );
  1.1209 +}

mercurial