1 /*
   2  * Copyright (c) 1997, 2021, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "compiler/compileLog.hpp"
  27 #include "gc/shared/barrierSet.hpp"
  28 #include "gc/shared/c2/barrierSetC2.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "opto/addnode.hpp"
  31 #include "opto/callnode.hpp"
  32 #include "opto/cfgnode.hpp"
  33 #include "opto/loopnode.hpp"
  34 #include "opto/matcher.hpp"
  35 #include "opto/movenode.hpp"
  36 #include "opto/mulnode.hpp"
  37 #include "opto/opcodes.hpp"
  38 #include "opto/phaseX.hpp"
  39 #include "opto/subnode.hpp"
  40 #include "runtime/sharedRuntime.hpp"
  41 
  42 // Portions of code courtesy of Clifford Click
  43 
  44 // Optimization - Graph Style
  45 
  46 #include "math.h"
  47 
  48 //=============================================================================
  49 //------------------------------Identity---------------------------------------
  50 // If right input is a constant 0, return the left input.
  51 Node* SubNode::Identity(PhaseGVN* phase) {
  52   assert(in(1) != this, "Must already have called Value");
  53   assert(in(2) != this, "Must already have called Value");
  54 
  55   // Remove double negation
  56   const Type *zero = add_id();
  57   if( phase->type( in(1) )->higher_equal( zero ) &&
  58       in(2)->Opcode() == Opcode() &&
  59       phase->type( in(2)->in(1) )->higher_equal( zero ) ) {
  60     return in(2)->in(2);
  61   }
  62 
  63   // Convert "(X+Y) - Y" into X and "(X+Y) - X" into Y
  64   if (in(1)->Opcode() == Op_AddI) {
  65     if (in(1)->in(2) == in(2)) {
  66       return in(1)->in(1);
  67     }
  68     if (in(1)->in(1) == in(2)) {
  69       return in(1)->in(2);
  70     }
  71 
  72     // Also catch: "(X + Opaque2(Y)) - Y".  In this case, 'Y' is a loop-varying
  73     // trip counter and X is likely to be loop-invariant (that's how O2 Nodes
  74     // are originally used, although the optimizer sometimes jiggers things).
  75     // This folding through an O2 removes a loop-exit use of a loop-varying
  76     // value and generally lowers register pressure in and around the loop.
  77     if (in(1)->in(2)->Opcode() == Op_Opaque2 && in(1)->in(2)->in(1) == in(2)) {
  78       return in(1)->in(1);
  79     }
  80   }
  81 
  82   return ( phase->type( in(2) )->higher_equal( zero ) ) ? in(1) : this;
  83 }
  84 
  85 //------------------------------Value------------------------------------------
  86 // A subtract node differences it's two inputs.
  87 const Type* SubNode::Value_common(PhaseTransform *phase) const {
  88   const Node* in1 = in(1);
  89   const Node* in2 = in(2);
  90   // Either input is TOP ==> the result is TOP
  91   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
  92   if( t1 == Type::TOP ) return Type::TOP;
  93   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
  94   if( t2 == Type::TOP ) return Type::TOP;
  95 
  96   // Not correct for SubFnode and AddFNode (must check for infinity)
  97   // Equal?  Subtract is zero
  98   if (in1->eqv_uncast(in2))  return add_id();
  99 
 100   // Either input is BOTTOM ==> the result is the local BOTTOM
 101   if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
 102     return bottom_type();
 103 
 104   return NULL;
 105 }
 106 
 107 const Type* SubNode::Value(PhaseGVN* phase) const {
 108   const Type* t = Value_common(phase);
 109   if (t != NULL) {
 110     return t;
 111   }
 112   const Type* t1 = phase->type(in(1));
 113   const Type* t2 = phase->type(in(2));
 114   return sub(t1,t2);            // Local flavor of type subtraction
 115 
 116 }
 117 
 118 SubNode* SubNode::make(Node* in1, Node* in2, BasicType bt) {
 119   switch (bt) {
 120     case T_INT:
 121       return new SubINode(in1, in2);
 122     case T_LONG:
 123       return new SubLNode(in1, in2);
 124     default:
 125       fatal("Not implemented for %s", type2name(bt));
 126   }
 127   return NULL;
 128 }
 129 
 130 //=============================================================================
 131 //------------------------------Helper function--------------------------------
 132 
 133 static bool is_cloop_increment(Node* inc) {
 134   precond(inc->Opcode() == Op_AddI || inc->Opcode() == Op_AddL);
 135 
 136   if (!inc->in(1)->is_Phi()) {
 137     return false;
 138   }
 139   const PhiNode* phi = inc->in(1)->as_Phi();
 140 
 141   if (!phi->region()->is_CountedLoop()) {
 142     return false;
 143   }
 144 
 145   return inc == phi->region()->as_CountedLoop()->incr();
 146 }
 147 
 148 // Given the expression '(x + C) - v', or
 149 //                      'v - (x + C)', we examine nodes '+' and 'v':
 150 //
 151 //  1. Do not convert if '+' is a counted-loop increment, because the '-' is
 152 //     loop invariant and converting extends the live-range of 'x' to overlap
 153 //     with the '+', forcing another register to be used in the loop.
 154 //
 155 //  2. Do not convert if 'v' is a counted-loop induction variable, because
 156 //     'x' might be invariant.
 157 //
 158 static bool ok_to_convert(Node* inc, Node* var) {
 159   return !(is_cloop_increment(inc) || var->is_cloop_ind_var());
 160 }
 161 
 162 //------------------------------Ideal------------------------------------------
 163 Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){
 164   Node *in1 = in(1);
 165   Node *in2 = in(2);
 166   uint op1 = in1->Opcode();
 167   uint op2 = in2->Opcode();
 168 
 169 #ifdef ASSERT
 170   // Check for dead loop
 171   if ((in1 == this) || (in2 == this) ||
 172       ((op1 == Op_AddI || op1 == Op_SubI) &&
 173        ((in1->in(1) == this) || (in1->in(2) == this) ||
 174         (in1->in(1) == in1)  || (in1->in(2) == in1)))) {
 175     assert(false, "dead loop in SubINode::Ideal");
 176   }
 177 #endif
 178 
 179   const Type *t2 = phase->type( in2 );
 180   if( t2 == Type::TOP ) return NULL;
 181   // Convert "x-c0" into "x+ -c0".
 182   if( t2->base() == Type::Int ){        // Might be bottom or top...
 183     const TypeInt *i = t2->is_int();
 184     if( i->is_con() )
 185       return new AddINode(in1, phase->intcon(-i->get_con()));
 186   }
 187 
 188   // Convert "(x+c0) - y" into (x-y) + c0"
 189   // Do not collapse (x+c0)-y if "+" is a loop increment or
 190   // if "y" is a loop induction variable.
 191   if( op1 == Op_AddI && ok_to_convert(in1, in2) ) {
 192     const Type *tadd = phase->type( in1->in(2) );
 193     if( tadd->singleton() && tadd != Type::TOP ) {
 194       Node *sub2 = phase->transform( new SubINode( in1->in(1), in2 ));
 195       return new AddINode( sub2, in1->in(2) );
 196     }
 197   }
 198 
 199 
 200   // Convert "x - (y+c0)" into "(x-y) - c0"
 201   // Need the same check as in above optimization but reversed.
 202   if (op2 == Op_AddI && ok_to_convert(in2, in1)) {
 203     Node* in21 = in2->in(1);
 204     Node* in22 = in2->in(2);
 205     const TypeInt* tcon = phase->type(in22)->isa_int();
 206     if (tcon != NULL && tcon->is_con()) {
 207       Node* sub2 = phase->transform( new SubINode(in1, in21) );
 208       Node* neg_c0 = phase->intcon(- tcon->get_con());
 209       return new AddINode(sub2, neg_c0);
 210     }
 211   }
 212 
 213   const Type *t1 = phase->type( in1 );
 214   if( t1 == Type::TOP ) return NULL;
 215 
 216 #ifdef ASSERT
 217   // Check for dead loop
 218   if ((op2 == Op_AddI || op2 == Op_SubI) &&
 219       ((in2->in(1) == this) || (in2->in(2) == this) ||
 220        (in2->in(1) == in2)  || (in2->in(2) == in2))) {
 221     assert(false, "dead loop in SubINode::Ideal");
 222   }
 223 #endif
 224 
 225   // Convert "x - (x+y)" into "-y"
 226   if (op2 == Op_AddI && in1 == in2->in(1)) {
 227     return new SubINode(phase->intcon(0), in2->in(2));
 228   }
 229   // Convert "(x-y) - x" into "-y"
 230   if (op1 == Op_SubI && in1->in(1) == in2) {
 231     return new SubINode(phase->intcon(0), in1->in(2));
 232   }
 233   // Convert "x - (y+x)" into "-y"
 234   if (op2 == Op_AddI && in1 == in2->in(2)) {
 235     return new SubINode(phase->intcon(0), in2->in(1));
 236   }
 237 
 238   // Convert "0 - (x-y)" into "y-x", leave the double negation "-(-y)" to SubNode::Identity().
 239   if (t1 == TypeInt::ZERO && op2 == Op_SubI && phase->type(in2->in(1)) != TypeInt::ZERO) {
 240     return new SubINode(in2->in(2), in2->in(1));
 241   }
 242 
 243   // Convert "0 - (x+con)" into "-con-x"
 244   jint con;
 245   if( t1 == TypeInt::ZERO && op2 == Op_AddI &&
 246       (con = in2->in(2)->find_int_con(0)) != 0 )
 247     return new SubINode( phase->intcon(-con), in2->in(1) );
 248 
 249   // Convert "(X+A) - (X+B)" into "A - B"
 250   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) )
 251     return new SubINode( in1->in(2), in2->in(2) );
 252 
 253   // Convert "(A+X) - (B+X)" into "A - B"
 254   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) )
 255     return new SubINode( in1->in(1), in2->in(1) );
 256 
 257   // Convert "(A+X) - (X+B)" into "A - B"
 258   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(1) )
 259     return new SubINode( in1->in(1), in2->in(2) );
 260 
 261   // Convert "(X+A) - (B+X)" into "A - B"
 262   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(2) )
 263     return new SubINode( in1->in(2), in2->in(1) );
 264 
 265   // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally
 266   // nicer to optimize than subtract.
 267   if( op2 == Op_SubI && in2->outcnt() == 1) {
 268     Node *add1 = phase->transform( new AddINode( in1, in2->in(2) ) );
 269     return new SubINode( add1, in2->in(1) );
 270   }
 271 
 272   // Associative
 273   if (op1 == Op_MulI && op2 == Op_MulI) {
 274     Node* sub_in1 = NULL;
 275     Node* sub_in2 = NULL;
 276     Node* mul_in = NULL;
 277 
 278     if (in1->in(1) == in2->in(1)) {
 279       // Convert "a*b-a*c into a*(b-c)
 280       sub_in1 = in1->in(2);
 281       sub_in2 = in2->in(2);
 282       mul_in = in1->in(1);
 283     } else if (in1->in(2) == in2->in(1)) {
 284       // Convert a*b-b*c into b*(a-c)
 285       sub_in1 = in1->in(1);
 286       sub_in2 = in2->in(2);
 287       mul_in = in1->in(2);
 288     } else if (in1->in(2) == in2->in(2)) {
 289       // Convert a*c-b*c into (a-b)*c
 290       sub_in1 = in1->in(1);
 291       sub_in2 = in2->in(1);
 292       mul_in = in1->in(2);
 293     } else if (in1->in(1) == in2->in(2)) {
 294       // Convert a*b-c*a into a*(b-c)
 295       sub_in1 = in1->in(2);
 296       sub_in2 = in2->in(1);
 297       mul_in = in1->in(1);
 298     }
 299 
 300     if (mul_in != NULL) {
 301       Node* sub = phase->transform(new SubINode(sub_in1, sub_in2));
 302       return new MulINode(mul_in, sub);
 303     }
 304   }
 305 
 306   // Convert "0-(A>>31)" into "(A>>>31)"
 307   if ( op2 == Op_RShiftI ) {
 308     Node *in21 = in2->in(1);
 309     Node *in22 = in2->in(2);
 310     const TypeInt *zero = phase->type(in1)->isa_int();
 311     const TypeInt *t21 = phase->type(in21)->isa_int();
 312     const TypeInt *t22 = phase->type(in22)->isa_int();
 313     if ( t21 && t22 && zero == TypeInt::ZERO && t22->is_con(31) ) {
 314       return new URShiftINode(in21, in22);
 315     }
 316   }
 317 
 318   return NULL;
 319 }
 320 
 321 //------------------------------sub--------------------------------------------
 322 // A subtract node differences it's two inputs.
 323 const Type *SubINode::sub( const Type *t1, const Type *t2 ) const {
 324   const TypeInt *r0 = t1->is_int(); // Handy access
 325   const TypeInt *r1 = t2->is_int();
 326   int32_t lo = java_subtract(r0->_lo, r1->_hi);
 327   int32_t hi = java_subtract(r0->_hi, r1->_lo);
 328 
 329   // We next check for 32-bit overflow.
 330   // If that happens, we just assume all integers are possible.
 331   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
 332        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
 333       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
 334        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
 335     return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen));
 336   else                          // Overflow; assume all integers
 337     return TypeInt::INT;
 338 }
 339 
 340 //=============================================================================
 341 //------------------------------Ideal------------------------------------------
 342 Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 343   Node *in1 = in(1);
 344   Node *in2 = in(2);
 345   uint op1 = in1->Opcode();
 346   uint op2 = in2->Opcode();
 347 
 348 #ifdef ASSERT
 349   // Check for dead loop
 350   if ((in1 == this) || (in2 == this) ||
 351       ((op1 == Op_AddL || op1 == Op_SubL) &&
 352        ((in1->in(1) == this) || (in1->in(2) == this) ||
 353         (in1->in(1) == in1)  || (in1->in(2) == in1)))) {
 354     assert(false, "dead loop in SubLNode::Ideal");
 355   }
 356 #endif
 357 
 358   if( phase->type( in2 ) == Type::TOP ) return NULL;
 359   const TypeLong *i = phase->type( in2 )->isa_long();
 360   // Convert "x-c0" into "x+ -c0".
 361   if( i &&                      // Might be bottom or top...
 362       i->is_con() )
 363     return new AddLNode(in1, phase->longcon(-i->get_con()));
 364 
 365   // Convert "(x+c0) - y" into (x-y) + c0"
 366   // Do not collapse (x+c0)-y if "+" is a loop increment or
 367   // if "y" is a loop induction variable.
 368   if( op1 == Op_AddL && ok_to_convert(in1, in2) ) {
 369     Node *in11 = in1->in(1);
 370     const Type *tadd = phase->type( in1->in(2) );
 371     if( tadd->singleton() && tadd != Type::TOP ) {
 372       Node *sub2 = phase->transform( new SubLNode( in11, in2 ));
 373       return new AddLNode( sub2, in1->in(2) );
 374     }
 375   }
 376 
 377   // Convert "x - (y+c0)" into "(x-y) - c0"
 378   // Need the same check as in above optimization but reversed.
 379   if (op2 == Op_AddL && ok_to_convert(in2, in1)) {
 380     Node* in21 = in2->in(1);
 381     Node* in22 = in2->in(2);
 382     const TypeLong* tcon = phase->type(in22)->isa_long();
 383     if (tcon != NULL && tcon->is_con()) {
 384       Node* sub2 = phase->transform( new SubLNode(in1, in21) );
 385       Node* neg_c0 = phase->longcon(- tcon->get_con());
 386       return new AddLNode(sub2, neg_c0);
 387     }
 388   }
 389 
 390   const Type *t1 = phase->type( in1 );
 391   if( t1 == Type::TOP ) return NULL;
 392 
 393 #ifdef ASSERT
 394   // Check for dead loop
 395   if ((op2 == Op_AddL || op2 == Op_SubL) &&
 396       ((in2->in(1) == this) || (in2->in(2) == this) ||
 397        (in2->in(1) == in2)  || (in2->in(2) == in2))) {
 398     assert(false, "dead loop in SubLNode::Ideal");
 399   }
 400 #endif
 401 
 402   // Convert "x - (x+y)" into "-y"
 403   if (op2 == Op_AddL && in1 == in2->in(1)) {
 404     return new SubLNode(phase->makecon(TypeLong::ZERO), in2->in(2));
 405   }
 406   // Convert "x - (y+x)" into "-y"
 407   if (op2 == Op_AddL && in1 == in2->in(2)) {
 408     return new SubLNode(phase->makecon(TypeLong::ZERO), in2->in(1));
 409   }
 410 
 411   // Convert "0 - (x-y)" into "y-x", leave the double negation "-(-y)" to SubNode::Identity.
 412   if (t1 == TypeLong::ZERO && op2 == Op_SubL && phase->type(in2->in(1)) != TypeLong::ZERO) {
 413     return new SubLNode(in2->in(2), in2->in(1));
 414   }
 415 
 416   // Convert "(X+A) - (X+B)" into "A - B"
 417   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) )
 418     return new SubLNode( in1->in(2), in2->in(2) );
 419 
 420   // Convert "(A+X) - (B+X)" into "A - B"
 421   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) )
 422     return new SubLNode( in1->in(1), in2->in(1) );
 423 
 424   // Convert "A-(B-C)" into (A+C)-B"
 425   if( op2 == Op_SubL && in2->outcnt() == 1) {
 426     Node *add1 = phase->transform( new AddLNode( in1, in2->in(2) ) );
 427     return new SubLNode( add1, in2->in(1) );
 428   }
 429 
 430   // Associative
 431   if (op1 == Op_MulL && op2 == Op_MulL) {
 432     Node* sub_in1 = NULL;
 433     Node* sub_in2 = NULL;
 434     Node* mul_in = NULL;
 435 
 436     if (in1->in(1) == in2->in(1)) {
 437       // Convert "a*b-a*c into a*(b+c)
 438       sub_in1 = in1->in(2);
 439       sub_in2 = in2->in(2);
 440       mul_in = in1->in(1);
 441     } else if (in1->in(2) == in2->in(1)) {
 442       // Convert a*b-b*c into b*(a-c)
 443       sub_in1 = in1->in(1);
 444       sub_in2 = in2->in(2);
 445       mul_in = in1->in(2);
 446     } else if (in1->in(2) == in2->in(2)) {
 447       // Convert a*c-b*c into (a-b)*c
 448       sub_in1 = in1->in(1);
 449       sub_in2 = in2->in(1);
 450       mul_in = in1->in(2);
 451     } else if (in1->in(1) == in2->in(2)) {
 452       // Convert a*b-c*a into a*(b-c)
 453       sub_in1 = in1->in(2);
 454       sub_in2 = in2->in(1);
 455       mul_in = in1->in(1);
 456     }
 457 
 458     if (mul_in != NULL) {
 459       Node* sub = phase->transform(new SubLNode(sub_in1, sub_in2));
 460       return new MulLNode(mul_in, sub);
 461     }
 462   }
 463 
 464   // Convert "0L-(A>>63)" into "(A>>>63)"
 465   if ( op2 == Op_RShiftL ) {
 466     Node *in21 = in2->in(1);
 467     Node *in22 = in2->in(2);
 468     const TypeLong *zero = phase->type(in1)->isa_long();
 469     const TypeLong *t21 = phase->type(in21)->isa_long();
 470     const TypeInt *t22 = phase->type(in22)->isa_int();
 471     if ( t21 && t22 && zero == TypeLong::ZERO && t22->is_con(63) ) {
 472       return new URShiftLNode(in21, in22);
 473     }
 474   }
 475 
 476   return NULL;
 477 }
 478 
 479 //------------------------------sub--------------------------------------------
 480 // A subtract node differences it's two inputs.
 481 const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const {
 482   const TypeLong *r0 = t1->is_long(); // Handy access
 483   const TypeLong *r1 = t2->is_long();
 484   jlong lo = java_subtract(r0->_lo, r1->_hi);
 485   jlong hi = java_subtract(r0->_hi, r1->_lo);
 486 
 487   // We next check for 32-bit overflow.
 488   // If that happens, we just assume all integers are possible.
 489   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
 490        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
 491       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
 492        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
 493     return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen));
 494   else                          // Overflow; assume all integers
 495     return TypeLong::LONG;
 496 }
 497 
 498 //=============================================================================
 499 //------------------------------Value------------------------------------------
 500 // A subtract node differences its two inputs.
 501 const Type* SubFPNode::Value(PhaseGVN* phase) const {
 502   const Node* in1 = in(1);
 503   const Node* in2 = in(2);
 504   // Either input is TOP ==> the result is TOP
 505   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
 506   if( t1 == Type::TOP ) return Type::TOP;
 507   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
 508   if( t2 == Type::TOP ) return Type::TOP;
 509 
 510   // if both operands are infinity of same sign, the result is NaN; do
 511   // not replace with zero
 512   if (t1->is_finite() && t2->is_finite() && in1 == in2) {
 513     return add_id();
 514   }
 515 
 516   // Either input is BOTTOM ==> the result is the local BOTTOM
 517   const Type *bot = bottom_type();
 518   if( (t1 == bot) || (t2 == bot) ||
 519       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 520     return bot;
 521 
 522   return sub(t1,t2);            // Local flavor of type subtraction
 523 }
 524 
 525 
 526 //=============================================================================
 527 //------------------------------Ideal------------------------------------------
 528 Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 529   const Type *t2 = phase->type( in(2) );
 530   // Convert "x-c0" into "x+ -c0".
 531   if( t2->base() == Type::FloatCon ) {  // Might be bottom or top...
 532     // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) );
 533   }
 534 
 535   // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes
 536   // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0.
 537   //if( phase->type(in(1)) == TypeF::ZERO )
 538   //return new (phase->C, 2) NegFNode(in(2));
 539 
 540   return NULL;
 541 }
 542 
 543 //------------------------------sub--------------------------------------------
 544 // A subtract node differences its two inputs.
 545 const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const {
 546   // no folding if one of operands is infinity or NaN, do not do constant folding
 547   if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) {
 548     return TypeF::make( t1->getf() - t2->getf() );
 549   }
 550   else if( g_isnan(t1->getf()) ) {
 551     return t1;
 552   }
 553   else if( g_isnan(t2->getf()) ) {
 554     return t2;
 555   }
 556   else {
 557     return Type::FLOAT;
 558   }
 559 }
 560 
 561 //=============================================================================
 562 //------------------------------Ideal------------------------------------------
 563 Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){
 564   const Type *t2 = phase->type( in(2) );
 565   // Convert "x-c0" into "x+ -c0".
 566   if( t2->base() == Type::DoubleCon ) { // Might be bottom or top...
 567     // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) );
 568   }
 569 
 570   // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes
 571   // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0.
 572   //if( phase->type(in(1)) == TypeD::ZERO )
 573   //return new (phase->C, 2) NegDNode(in(2));
 574 
 575   return NULL;
 576 }
 577 
 578 //------------------------------sub--------------------------------------------
 579 // A subtract node differences its two inputs.
 580 const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const {
 581   // no folding if one of operands is infinity or NaN, do not do constant folding
 582   if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) {
 583     return TypeD::make( t1->getd() - t2->getd() );
 584   }
 585   else if( g_isnan(t1->getd()) ) {
 586     return t1;
 587   }
 588   else if( g_isnan(t2->getd()) ) {
 589     return t2;
 590   }
 591   else {
 592     return Type::DOUBLE;
 593   }
 594 }
 595 
 596 //=============================================================================
 597 //------------------------------Idealize---------------------------------------
 598 // Unlike SubNodes, compare must still flatten return value to the
 599 // range -1, 0, 1.
 600 // And optimizations like those for (X + Y) - X fail if overflow happens.
 601 Node* CmpNode::Identity(PhaseGVN* phase) {
 602   return this;
 603 }
 604 
 605 #ifndef PRODUCT
 606 //----------------------------related------------------------------------------
 607 // Related nodes of comparison nodes include all data inputs (until hitting a
 608 // control boundary) as well as all outputs until and including control nodes
 609 // as well as their projections. In compact mode, data inputs till depth 1 and
 610 // all outputs till depth 1 are considered.
 611 void CmpNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {
 612   if (compact) {
 613     this->collect_nodes(in_rel, 1, false, true);
 614     this->collect_nodes(out_rel, -1, false, false);
 615   } else {
 616     this->collect_nodes_in_all_data(in_rel, false);
 617     this->collect_nodes_out_all_ctrl_boundary(out_rel);
 618     // Now, find all control nodes in out_rel, and include their projections
 619     // and projection targets (if any) in the result.
 620     GrowableArray<Node*> proj(Compile::current()->unique());
 621     for (GrowableArrayIterator<Node*> it = out_rel->begin(); it != out_rel->end(); ++it) {
 622       Node* n = *it;
 623       if (n->is_CFG() && !n->is_Proj()) {
 624         // Assume projections and projection targets are found at levels 1 and 2.
 625         n->collect_nodes(&proj, -2, false, false);
 626         for (GrowableArrayIterator<Node*> p = proj.begin(); p != proj.end(); ++p) {
 627           out_rel->append_if_missing(*p);
 628         }
 629         proj.clear();
 630       }
 631     }
 632   }
 633 }
 634 
 635 #endif
 636 
 637 CmpNode *CmpNode::make(Node *in1, Node *in2, BasicType bt, bool unsigned_comp) {
 638   switch (bt) {
 639     case T_INT:
 640       if (unsigned_comp) {
 641         return new CmpUNode(in1, in2);
 642       }
 643       return new CmpINode(in1, in2);
 644     case T_LONG:
 645       if (unsigned_comp) {
 646         return new CmpULNode(in1, in2);
 647       }
 648       return new CmpLNode(in1, in2);
 649     default:
 650       fatal("Not implemented for %s", type2name(bt));
 651   }
 652   return NULL;
 653 }
 654 
 655 //=============================================================================
 656 //------------------------------cmp--------------------------------------------
 657 // Simplify a CmpI (compare 2 integers) node, based on local information.
 658 // If both inputs are constants, compare them.
 659 const Type *CmpINode::sub( const Type *t1, const Type *t2 ) const {
 660   const TypeInt *r0 = t1->is_int(); // Handy access
 661   const TypeInt *r1 = t2->is_int();
 662 
 663   if( r0->_hi < r1->_lo )       // Range is always low?
 664     return TypeInt::CC_LT;
 665   else if( r0->_lo > r1->_hi )  // Range is always high?
 666     return TypeInt::CC_GT;
 667 
 668   else if( r0->is_con() && r1->is_con() ) { // comparing constants?
 669     assert(r0->get_con() == r1->get_con(), "must be equal");
 670     return TypeInt::CC_EQ;      // Equal results.
 671   } else if( r0->_hi == r1->_lo ) // Range is never high?
 672     return TypeInt::CC_LE;
 673   else if( r0->_lo == r1->_hi ) // Range is never low?
 674     return TypeInt::CC_GE;
 675   return TypeInt::CC;           // else use worst case results
 676 }
 677 
 678 // Simplify a CmpU (compare 2 integers) node, based on local information.
 679 // If both inputs are constants, compare them.
 680 const Type *CmpUNode::sub( const Type *t1, const Type *t2 ) const {
 681   assert(!t1->isa_ptr(), "obsolete usage of CmpU");
 682 
 683   // comparing two unsigned ints
 684   const TypeInt *r0 = t1->is_int();   // Handy access
 685   const TypeInt *r1 = t2->is_int();
 686 
 687   // Current installed version
 688   // Compare ranges for non-overlap
 689   juint lo0 = r0->_lo;
 690   juint hi0 = r0->_hi;
 691   juint lo1 = r1->_lo;
 692   juint hi1 = r1->_hi;
 693 
 694   // If either one has both negative and positive values,
 695   // it therefore contains both 0 and -1, and since [0..-1] is the
 696   // full unsigned range, the type must act as an unsigned bottom.
 697   bool bot0 = ((jint)(lo0 ^ hi0) < 0);
 698   bool bot1 = ((jint)(lo1 ^ hi1) < 0);
 699 
 700   if (bot0 || bot1) {
 701     // All unsigned values are LE -1 and GE 0.
 702     if (lo0 == 0 && hi0 == 0) {
 703       return TypeInt::CC_LE;            //   0 <= bot
 704     } else if ((jint)lo0 == -1 && (jint)hi0 == -1) {
 705       return TypeInt::CC_GE;            // -1 >= bot
 706     } else if (lo1 == 0 && hi1 == 0) {
 707       return TypeInt::CC_GE;            // bot >= 0
 708     } else if ((jint)lo1 == -1 && (jint)hi1 == -1) {
 709       return TypeInt::CC_LE;            // bot <= -1
 710     }
 711   } else {
 712     // We can use ranges of the form [lo..hi] if signs are the same.
 713     assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
 714     // results are reversed, '-' > '+' for unsigned compare
 715     if (hi0 < lo1) {
 716       return TypeInt::CC_LT;            // smaller
 717     } else if (lo0 > hi1) {
 718       return TypeInt::CC_GT;            // greater
 719     } else if (hi0 == lo1 && lo0 == hi1) {
 720       return TypeInt::CC_EQ;            // Equal results
 721     } else if (lo0 >= hi1) {
 722       return TypeInt::CC_GE;
 723     } else if (hi0 <= lo1) {
 724       // Check for special case in Hashtable::get.  (See below.)
 725       if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check())
 726         return TypeInt::CC_LT;
 727       return TypeInt::CC_LE;
 728     }
 729   }
 730   // Check for special case in Hashtable::get - the hash index is
 731   // mod'ed to the table size so the following range check is useless.
 732   // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have
 733   // to be positive.
 734   // (This is a gross hack, since the sub method never
 735   // looks at the structure of the node in any other case.)
 736   if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check())
 737     return TypeInt::CC_LT;
 738   return TypeInt::CC;                   // else use worst case results
 739 }
 740 
 741 const Type* CmpUNode::Value(PhaseGVN* phase) const {
 742   const Type* t = SubNode::Value_common(phase);
 743   if (t != NULL) {
 744     return t;
 745   }
 746   const Node* in1 = in(1);
 747   const Node* in2 = in(2);
 748   const Type* t1 = phase->type(in1);
 749   const Type* t2 = phase->type(in2);
 750   assert(t1->isa_int(), "CmpU has only Int type inputs");
 751   if (t2 == TypeInt::INT) { // Compare to bottom?
 752     return bottom_type();
 753   }
 754   uint in1_op = in1->Opcode();
 755   if (in1_op == Op_AddI || in1_op == Op_SubI) {
 756     // The problem rise when result of AddI(SubI) may overflow
 757     // signed integer value. Let say the input type is
 758     // [256, maxint] then +128 will create 2 ranges due to
 759     // overflow: [minint, minint+127] and [384, maxint].
 760     // But C2 type system keep only 1 type range and as result
 761     // it use general [minint, maxint] for this case which we
 762     // can't optimize.
 763     //
 764     // Make 2 separate type ranges based on types of AddI(SubI) inputs
 765     // and compare results of their compare. If results are the same
 766     // CmpU node can be optimized.
 767     const Node* in11 = in1->in(1);
 768     const Node* in12 = in1->in(2);
 769     const Type* t11 = (in11 == in1) ? Type::TOP : phase->type(in11);
 770     const Type* t12 = (in12 == in1) ? Type::TOP : phase->type(in12);
 771     // Skip cases when input types are top or bottom.
 772     if ((t11 != Type::TOP) && (t11 != TypeInt::INT) &&
 773         (t12 != Type::TOP) && (t12 != TypeInt::INT)) {
 774       const TypeInt *r0 = t11->is_int();
 775       const TypeInt *r1 = t12->is_int();
 776       jlong lo_r0 = r0->_lo;
 777       jlong hi_r0 = r0->_hi;
 778       jlong lo_r1 = r1->_lo;
 779       jlong hi_r1 = r1->_hi;
 780       if (in1_op == Op_SubI) {
 781         jlong tmp = hi_r1;
 782         hi_r1 = -lo_r1;
 783         lo_r1 = -tmp;
 784         // Note, for substructing [minint,x] type range
 785         // long arithmetic provides correct overflow answer.
 786         // The confusion come from the fact that in 32-bit
 787         // -minint == minint but in 64-bit -minint == maxint+1.
 788       }
 789       jlong lo_long = lo_r0 + lo_r1;
 790       jlong hi_long = hi_r0 + hi_r1;
 791       int lo_tr1 = min_jint;
 792       int hi_tr1 = (int)hi_long;
 793       int lo_tr2 = (int)lo_long;
 794       int hi_tr2 = max_jint;
 795       bool underflow = lo_long != (jlong)lo_tr2;
 796       bool overflow  = hi_long != (jlong)hi_tr1;
 797       // Use sub(t1, t2) when there is no overflow (one type range)
 798       // or when both overflow and underflow (too complex).
 799       if ((underflow != overflow) && (hi_tr1 < lo_tr2)) {
 800         // Overflow only on one boundary, compare 2 separate type ranges.
 801         int w = MAX2(r0->_widen, r1->_widen); // _widen does not matter here
 802         const TypeInt* tr1 = TypeInt::make(lo_tr1, hi_tr1, w);
 803         const TypeInt* tr2 = TypeInt::make(lo_tr2, hi_tr2, w);
 804         const Type* cmp1 = sub(tr1, t2);
 805         const Type* cmp2 = sub(tr2, t2);
 806         if (cmp1 == cmp2) {
 807           return cmp1; // Hit!
 808         }
 809       }
 810     }
 811   }
 812 
 813   return sub(t1, t2);            // Local flavor of type subtraction
 814 }
 815 
 816 bool CmpUNode::is_index_range_check() const {
 817   // Check for the "(X ModI Y) CmpU Y" shape
 818   return (in(1)->Opcode() == Op_ModI &&
 819           in(1)->in(2)->eqv_uncast(in(2)));
 820 }
 821 
 822 //------------------------------Idealize---------------------------------------
 823 Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
 824   if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
 825     switch (in(1)->Opcode()) {
 826     case Op_CmpL3:              // Collapse a CmpL3/CmpI into a CmpL
 827       return new CmpLNode(in(1)->in(1),in(1)->in(2));
 828     case Op_CmpF3:              // Collapse a CmpF3/CmpI into a CmpF
 829       return new CmpFNode(in(1)->in(1),in(1)->in(2));
 830     case Op_CmpD3:              // Collapse a CmpD3/CmpI into a CmpD
 831       return new CmpDNode(in(1)->in(1),in(1)->in(2));
 832     //case Op_SubI:
 833       // If (x - y) cannot overflow, then ((x - y) <?> 0)
 834       // can be turned into (x <?> y).
 835       // This is handled (with more general cases) by Ideal_sub_algebra.
 836     }
 837   }
 838   return NULL;                  // No change
 839 }
 840 
 841 Node *CmpLNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
 842   const TypeLong *t2 = phase->type(in(2))->isa_long();
 843   if (Opcode() == Op_CmpL && in(1)->Opcode() == Op_ConvI2L && t2 && t2->is_con()) {
 844     const jlong con = t2->get_con();
 845     if (con >= min_jint && con <= max_jint) {
 846       return new CmpINode(in(1)->in(1), phase->intcon((jint)con));
 847     }
 848   }
 849   return NULL;
 850 }
 851 
 852 //=============================================================================
 853 // Simplify a CmpL (compare 2 longs ) node, based on local information.
 854 // If both inputs are constants, compare them.
 855 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
 856   const TypeLong *r0 = t1->is_long(); // Handy access
 857   const TypeLong *r1 = t2->is_long();
 858 
 859   if( r0->_hi < r1->_lo )       // Range is always low?
 860     return TypeInt::CC_LT;
 861   else if( r0->_lo > r1->_hi )  // Range is always high?
 862     return TypeInt::CC_GT;
 863 
 864   else if( r0->is_con() && r1->is_con() ) { // comparing constants?
 865     assert(r0->get_con() == r1->get_con(), "must be equal");
 866     return TypeInt::CC_EQ;      // Equal results.
 867   } else if( r0->_hi == r1->_lo ) // Range is never high?
 868     return TypeInt::CC_LE;
 869   else if( r0->_lo == r1->_hi ) // Range is never low?
 870     return TypeInt::CC_GE;
 871   return TypeInt::CC;           // else use worst case results
 872 }
 873 
 874 
 875 // Simplify a CmpUL (compare 2 unsigned longs) node, based on local information.
 876 // If both inputs are constants, compare them.
 877 const Type* CmpULNode::sub(const Type* t1, const Type* t2) const {
 878   assert(!t1->isa_ptr(), "obsolete usage of CmpUL");
 879 
 880   // comparing two unsigned longs
 881   const TypeLong* r0 = t1->is_long();   // Handy access
 882   const TypeLong* r1 = t2->is_long();
 883 
 884   // Current installed version
 885   // Compare ranges for non-overlap
 886   julong lo0 = r0->_lo;
 887   julong hi0 = r0->_hi;
 888   julong lo1 = r1->_lo;
 889   julong hi1 = r1->_hi;
 890 
 891   // If either one has both negative and positive values,
 892   // it therefore contains both 0 and -1, and since [0..-1] is the
 893   // full unsigned range, the type must act as an unsigned bottom.
 894   bool bot0 = ((jlong)(lo0 ^ hi0) < 0);
 895   bool bot1 = ((jlong)(lo1 ^ hi1) < 0);
 896 
 897   if (bot0 || bot1) {
 898     // All unsigned values are LE -1 and GE 0.
 899     if (lo0 == 0 && hi0 == 0) {
 900       return TypeInt::CC_LE;            //   0 <= bot
 901     } else if ((jlong)lo0 == -1 && (jlong)hi0 == -1) {
 902       return TypeInt::CC_GE;            // -1 >= bot
 903     } else if (lo1 == 0 && hi1 == 0) {
 904       return TypeInt::CC_GE;            // bot >= 0
 905     } else if ((jlong)lo1 == -1 && (jlong)hi1 == -1) {
 906       return TypeInt::CC_LE;            // bot <= -1
 907     }
 908   } else {
 909     // We can use ranges of the form [lo..hi] if signs are the same.
 910     assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
 911     // results are reversed, '-' > '+' for unsigned compare
 912     if (hi0 < lo1) {
 913       return TypeInt::CC_LT;            // smaller
 914     } else if (lo0 > hi1) {
 915       return TypeInt::CC_GT;            // greater
 916     } else if (hi0 == lo1 && lo0 == hi1) {
 917       return TypeInt::CC_EQ;            // Equal results
 918     } else if (lo0 >= hi1) {
 919       return TypeInt::CC_GE;
 920     } else if (hi0 <= lo1) {
 921       return TypeInt::CC_LE;
 922     }
 923   }
 924 
 925   return TypeInt::CC;                   // else use worst case results
 926 }
 927 
 928 //=============================================================================
 929 //------------------------------sub--------------------------------------------
 930 // Simplify an CmpP (compare 2 pointers) node, based on local information.
 931 // If both inputs are constants, compare them.
 932 const Type *CmpPNode::sub( const Type *t1, const Type *t2 ) const {
 933   const TypePtr *r0 = t1->is_ptr(); // Handy access
 934   const TypePtr *r1 = t2->is_ptr();
 935 
 936   // Undefined inputs makes for an undefined result
 937   if( TypePtr::above_centerline(r0->_ptr) ||
 938       TypePtr::above_centerline(r1->_ptr) )
 939     return Type::TOP;
 940 
 941   if (r0 == r1 && r0->singleton()) {
 942     // Equal pointer constants (klasses, nulls, etc.)
 943     return TypeInt::CC_EQ;
 944   }
 945 
 946   // See if it is 2 unrelated classes.
 947   const TypeOopPtr* oop_p0 = r0->isa_oopptr();
 948   const TypeOopPtr* oop_p1 = r1->isa_oopptr();
 949   bool both_oop_ptr = oop_p0 && oop_p1;
 950 
 951   if (both_oop_ptr) {
 952     Node* in1 = in(1)->uncast();
 953     Node* in2 = in(2)->uncast();
 954     AllocateNode* alloc1 = AllocateNode::Ideal_allocation(in1, NULL);
 955     AllocateNode* alloc2 = AllocateNode::Ideal_allocation(in2, NULL);
 956     if (MemNode::detect_ptr_independence(in1, alloc1, in2, alloc2, NULL)) {
 957       return TypeInt::CC_GT;  // different pointers
 958     }
 959   }
 960 
 961   const TypeKlassPtr* klass_p0 = r0->isa_klassptr();
 962   const TypeKlassPtr* klass_p1 = r1->isa_klassptr();
 963 
 964   if (both_oop_ptr || (klass_p0 && klass_p1)) { // both or neither are klass pointers
 965     ciKlass* klass0 = NULL;
 966     bool    xklass0 = false;
 967     ciKlass* klass1 = NULL;
 968     bool    xklass1 = false;
 969 
 970     if (oop_p0) {
 971       klass0 = oop_p0->klass();
 972       xklass0 = oop_p0->klass_is_exact();
 973     } else {
 974       assert(klass_p0, "must be non-null if oop_p0 is null");
 975       klass0 = klass_p0->klass();
 976       xklass0 = klass_p0->klass_is_exact();
 977     }
 978 
 979     if (oop_p1) {
 980       klass1 = oop_p1->klass();
 981       xklass1 = oop_p1->klass_is_exact();
 982     } else {
 983       assert(klass_p1, "must be non-null if oop_p1 is null");
 984       klass1 = klass_p1->klass();
 985       xklass1 = klass_p1->klass_is_exact();
 986     }
 987 
 988     if (klass0 && klass1 &&
 989         klass0->is_loaded() && !klass0->is_interface() && // do not trust interfaces
 990         klass1->is_loaded() && !klass1->is_interface() &&
 991         (!klass0->is_obj_array_klass() ||
 992          !klass0->as_obj_array_klass()->base_element_klass()->is_interface()) &&
 993         (!klass1->is_obj_array_klass() ||
 994          !klass1->as_obj_array_klass()->base_element_klass()->is_interface())) {
 995       bool unrelated_classes = false;
 996       // See if neither subclasses the other, or if the class on top
 997       // is precise.  In either of these cases, the compare is known
 998       // to fail if at least one of the pointers is provably not null.
 999       if (klass0->equals(klass1)) {  // if types are unequal but klasses are equal
1000         // Do nothing; we know nothing for imprecise types
1001       } else if (klass0->is_subtype_of(klass1)) {
1002         // If klass1's type is PRECISE, then classes are unrelated.
1003         unrelated_classes = xklass1;
1004       } else if (klass1->is_subtype_of(klass0)) {
1005         // If klass0's type is PRECISE, then classes are unrelated.
1006         unrelated_classes = xklass0;
1007       } else {                  // Neither subtypes the other
1008         unrelated_classes = true;
1009       }
1010       if (unrelated_classes) {
1011         // The oops classes are known to be unrelated. If the joined PTRs of
1012         // two oops is not Null and not Bottom, then we are sure that one
1013         // of the two oops is non-null, and the comparison will always fail.
1014         TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
1015         if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
1016           return TypeInt::CC_GT;
1017         }
1018       }
1019     }
1020   }
1021 
1022   // Known constants can be compared exactly
1023   // Null can be distinguished from any NotNull pointers
1024   // Unknown inputs makes an unknown result
1025   if( r0->singleton() ) {
1026     intptr_t bits0 = r0->get_con();
1027     if( r1->singleton() )
1028       return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
1029     return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
1030   } else if( r1->singleton() ) {
1031     intptr_t bits1 = r1->get_con();
1032     return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
1033   } else
1034     return TypeInt::CC;
1035 }
1036 
1037 static inline Node* isa_java_mirror_load(PhaseGVN* phase, Node* n) {
1038   // Return the klass node for (indirect load from OopHandle)
1039   //   LoadBarrier?(LoadP(LoadP(AddP(foo:Klass, #java_mirror))))
1040   //   or NULL if not matching.
1041   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1042     n = bs->step_over_gc_barrier(n);
1043 
1044   if (n->Opcode() != Op_LoadP) return NULL;
1045 
1046   const TypeInstPtr* tp = phase->type(n)->isa_instptr();
1047   if (!tp || tp->klass() != phase->C->env()->Class_klass()) return NULL;
1048 
1049   Node* adr = n->in(MemNode::Address);
1050   // First load from OopHandle: ((OopHandle)mirror)->resolve(); may need barrier.
1051   if (adr->Opcode() != Op_LoadP || !phase->type(adr)->isa_rawptr()) return NULL;
1052   adr = adr->in(MemNode::Address);
1053 
1054   intptr_t off = 0;
1055   Node* k = AddPNode::Ideal_base_and_offset(adr, phase, off);
1056   if (k == NULL)  return NULL;
1057   const TypeKlassPtr* tkp = phase->type(k)->isa_klassptr();
1058   if (!tkp || off != in_bytes(Klass::java_mirror_offset())) return NULL;
1059 
1060   // We've found the klass node of a Java mirror load.
1061   return k;
1062 }
1063 
1064 static inline Node* isa_const_java_mirror(PhaseGVN* phase, Node* n) {
1065   // for ConP(Foo.class) return ConP(Foo.klass)
1066   // otherwise return NULL
1067   if (!n->is_Con()) return NULL;
1068 
1069   const TypeInstPtr* tp = phase->type(n)->isa_instptr();
1070   if (!tp) return NULL;
1071 
1072   ciType* mirror_type = tp->java_mirror_type();
1073   // TypeInstPtr::java_mirror_type() returns non-NULL for compile-
1074   // time Class constants only.
1075   if (!mirror_type) return NULL;
1076 
1077   // x.getClass() == int.class can never be true (for all primitive types)
1078   // Return a ConP(NULL) node for this case.
1079   if (mirror_type->is_classless()) {
1080     return phase->makecon(TypePtr::NULL_PTR);
1081   }
1082 
1083   // return the ConP(Foo.klass)
1084   assert(mirror_type->is_klass(), "mirror_type should represent a Klass*");
1085   return phase->makecon(TypeKlassPtr::make(mirror_type->as_klass()));
1086 }
1087 
1088 //------------------------------Ideal------------------------------------------
1089 // Normalize comparisons between Java mirror loads to compare the klass instead.
1090 //
1091 // Also check for the case of comparing an unknown klass loaded from the primary
1092 // super-type array vs a known klass with no subtypes.  This amounts to
1093 // checking to see an unknown klass subtypes a known klass with no subtypes;
1094 // this only happens on an exact match.  We can shorten this test by 1 load.
1095 Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
1096   // Normalize comparisons between Java mirrors into comparisons of the low-
1097   // level klass, where a dependent load could be shortened.
1098   //
1099   // The new pattern has a nice effect of matching the same pattern used in the
1100   // fast path of instanceof/checkcast/Class.isInstance(), which allows
1101   // redundant exact type check be optimized away by GVN.
1102   // For example, in
1103   //   if (x.getClass() == Foo.class) {
1104   //     Foo foo = (Foo) x;
1105   //     // ... use a ...
1106   //   }
1107   // a CmpPNode could be shared between if_acmpne and checkcast
1108   {
1109     Node* k1 = isa_java_mirror_load(phase, in(1));
1110     Node* k2 = isa_java_mirror_load(phase, in(2));
1111     Node* conk2 = isa_const_java_mirror(phase, in(2));
1112 
1113     if (k1 && (k2 || conk2)) {
1114       Node* lhs = k1;
1115       Node* rhs = (k2 != NULL) ? k2 : conk2;
1116       set_req_X(1, lhs, phase);
1117       set_req_X(2, rhs, phase);
1118       return this;
1119     }
1120   }
1121 
1122   // Constant pointer on right?
1123   const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
1124   if (t2 == NULL || !t2->klass_is_exact())
1125     return NULL;
1126   // Get the constant klass we are comparing to.
1127   ciKlass* superklass = t2->klass();
1128 
1129   // Now check for LoadKlass on left.
1130   Node* ldk1 = in(1);
1131   if (ldk1->is_DecodeNKlass()) {
1132     ldk1 = ldk1->in(1);
1133     if (ldk1->Opcode() != Op_LoadNKlass )
1134       return NULL;
1135   } else if (ldk1->Opcode() != Op_LoadKlass )
1136     return NULL;
1137   // Take apart the address of the LoadKlass:
1138   Node* adr1 = ldk1->in(MemNode::Address);
1139   intptr_t con2 = 0;
1140   Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2);
1141   if (ldk2 == NULL)
1142     return NULL;
1143   if (con2 == oopDesc::klass_offset_in_bytes()) {
1144     // We are inspecting an object's concrete class.
1145     // Short-circuit the check if the query is abstract.
1146     if (superklass->is_interface() ||
1147         superklass->is_abstract()) {
1148       // Make it come out always false:
1149       this->set_req(2, phase->makecon(TypePtr::NULL_PTR));
1150       return this;
1151     }
1152   }
1153 
1154   // Check for a LoadKlass from primary supertype array.
1155   // Any nested loadklass from loadklass+con must be from the p.s. array.
1156   if (ldk2->is_DecodeNKlass()) {
1157     // Keep ldk2 as DecodeN since it could be used in CmpP below.
1158     if (ldk2->in(1)->Opcode() != Op_LoadNKlass )
1159       return NULL;
1160   } else if (ldk2->Opcode() != Op_LoadKlass)
1161     return NULL;
1162 
1163   // Verify that we understand the situation
1164   if (con2 != (intptr_t) superklass->super_check_offset())
1165     return NULL;                // Might be element-klass loading from array klass
1166 
1167   // If 'superklass' has no subklasses and is not an interface, then we are
1168   // assured that the only input which will pass the type check is
1169   // 'superklass' itself.
1170   //
1171   // We could be more liberal here, and allow the optimization on interfaces
1172   // which have a single implementor.  This would require us to increase the
1173   // expressiveness of the add_dependency() mechanism.
1174   // %%% Do this after we fix TypeOopPtr:  Deps are expressive enough now.
1175 
1176   // Object arrays must have their base element have no subtypes
1177   while (superklass->is_obj_array_klass()) {
1178     ciType* elem = superklass->as_obj_array_klass()->element_type();
1179     superklass = elem->as_klass();
1180   }
1181   if (superklass->is_instance_klass()) {
1182     ciInstanceKlass* ik = superklass->as_instance_klass();
1183     if (ik->has_subklass() || ik->is_interface())  return NULL;
1184     // Add a dependency if there is a chance that a subclass will be added later.
1185     if (!ik->is_final()) {
1186       phase->C->dependencies()->assert_leaf_type(ik);
1187     }
1188   }
1189 
1190   // Bypass the dependent load, and compare directly
1191   this->set_req(1,ldk2);
1192 
1193   return this;
1194 }
1195 
1196 //=============================================================================
1197 //------------------------------sub--------------------------------------------
1198 // Simplify an CmpN (compare 2 pointers) node, based on local information.
1199 // If both inputs are constants, compare them.
1200 const Type *CmpNNode::sub( const Type *t1, const Type *t2 ) const {
1201   ShouldNotReachHere();
1202   return bottom_type();
1203 }
1204 
1205 //------------------------------Ideal------------------------------------------
1206 Node *CmpNNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
1207   return NULL;
1208 }
1209 
1210 //=============================================================================
1211 //------------------------------Value------------------------------------------
1212 // Simplify an CmpF (compare 2 floats ) node, based on local information.
1213 // If both inputs are constants, compare them.
1214 const Type* CmpFNode::Value(PhaseGVN* phase) const {
1215   const Node* in1 = in(1);
1216   const Node* in2 = in(2);
1217   // Either input is TOP ==> the result is TOP
1218   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
1219   if( t1 == Type::TOP ) return Type::TOP;
1220   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
1221   if( t2 == Type::TOP ) return Type::TOP;
1222 
1223   // Not constants?  Don't know squat - even if they are the same
1224   // value!  If they are NaN's they compare to LT instead of EQ.
1225   const TypeF *tf1 = t1->isa_float_constant();
1226   const TypeF *tf2 = t2->isa_float_constant();
1227   if( !tf1 || !tf2 ) return TypeInt::CC;
1228 
1229   // This implements the Java bytecode fcmpl, so unordered returns -1.
1230   if( tf1->is_nan() || tf2->is_nan() )
1231     return TypeInt::CC_LT;
1232 
1233   if( tf1->_f < tf2->_f ) return TypeInt::CC_LT;
1234   if( tf1->_f > tf2->_f ) return TypeInt::CC_GT;
1235   assert( tf1->_f == tf2->_f, "do not understand FP behavior" );
1236   return TypeInt::CC_EQ;
1237 }
1238 
1239 
1240 //=============================================================================
1241 //------------------------------Value------------------------------------------
1242 // Simplify an CmpD (compare 2 doubles ) node, based on local information.
1243 // If both inputs are constants, compare them.
1244 const Type* CmpDNode::Value(PhaseGVN* phase) const {
1245   const Node* in1 = in(1);
1246   const Node* in2 = in(2);
1247   // Either input is TOP ==> the result is TOP
1248   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
1249   if( t1 == Type::TOP ) return Type::TOP;
1250   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
1251   if( t2 == Type::TOP ) return Type::TOP;
1252 
1253   // Not constants?  Don't know squat - even if they are the same
1254   // value!  If they are NaN's they compare to LT instead of EQ.
1255   const TypeD *td1 = t1->isa_double_constant();
1256   const TypeD *td2 = t2->isa_double_constant();
1257   if( !td1 || !td2 ) return TypeInt::CC;
1258 
1259   // This implements the Java bytecode dcmpl, so unordered returns -1.
1260   if( td1->is_nan() || td2->is_nan() )
1261     return TypeInt::CC_LT;
1262 
1263   if( td1->_d < td2->_d ) return TypeInt::CC_LT;
1264   if( td1->_d > td2->_d ) return TypeInt::CC_GT;
1265   assert( td1->_d == td2->_d, "do not understand FP behavior" );
1266   return TypeInt::CC_EQ;
1267 }
1268 
1269 //------------------------------Ideal------------------------------------------
1270 Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){
1271   // Check if we can change this to a CmpF and remove a ConvD2F operation.
1272   // Change  (CMPD (F2D (float)) (ConD value))
1273   // To      (CMPF      (float)  (ConF value))
1274   // Valid when 'value' does not lose precision as a float.
1275   // Benefits: eliminates conversion, does not require 24-bit mode
1276 
1277   // NaNs prevent commuting operands.  This transform works regardless of the
1278   // order of ConD and ConvF2D inputs by preserving the original order.
1279   int idx_f2d = 1;              // ConvF2D on left side?
1280   if( in(idx_f2d)->Opcode() != Op_ConvF2D )
1281     idx_f2d = 2;                // No, swap to check for reversed args
1282   int idx_con = 3-idx_f2d;      // Check for the constant on other input
1283 
1284   if( ConvertCmpD2CmpF &&
1285       in(idx_f2d)->Opcode() == Op_ConvF2D &&
1286       in(idx_con)->Opcode() == Op_ConD ) {
1287     const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
1288     double t2_value_as_double = t2->_d;
1289     float  t2_value_as_float  = (float)t2_value_as_double;
1290     if( t2_value_as_double == (double)t2_value_as_float ) {
1291       // Test value can be represented as a float
1292       // Eliminate the conversion to double and create new comparison
1293       Node *new_in1 = in(idx_f2d)->in(1);
1294       Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
1295       if( idx_f2d != 1 ) {      // Must flip args to match original order
1296         Node *tmp = new_in1;
1297         new_in1 = new_in2;
1298         new_in2 = tmp;
1299       }
1300       CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
1301         ? new CmpF3Node( new_in1, new_in2 )
1302         : new CmpFNode ( new_in1, new_in2 ) ;
1303       return new_cmp;           // Changed to CmpFNode
1304     }
1305     // Testing value required the precision of a double
1306   }
1307   return NULL;                  // No change
1308 }
1309 
1310 
1311 //=============================================================================
1312 //------------------------------cc2logical-------------------------------------
1313 // Convert a condition code type to a logical type
1314 const Type *BoolTest::cc2logical( const Type *CC ) const {
1315   if( CC == Type::TOP ) return Type::TOP;
1316   if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
1317   const TypeInt *ti = CC->is_int();
1318   if( ti->is_con() ) {          // Only 1 kind of condition codes set?
1319     // Match low order 2 bits
1320     int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
1321     if( _test & 4 ) tmp = 1-tmp;     // Optionally complement result
1322     return TypeInt::make(tmp);       // Boolean result
1323   }
1324 
1325   if( CC == TypeInt::CC_GE ) {
1326     if( _test == ge ) return TypeInt::ONE;
1327     if( _test == lt ) return TypeInt::ZERO;
1328   }
1329   if( CC == TypeInt::CC_LE ) {
1330     if( _test == le ) return TypeInt::ONE;
1331     if( _test == gt ) return TypeInt::ZERO;
1332   }
1333 
1334   return TypeInt::BOOL;
1335 }
1336 
1337 //------------------------------dump_spec-------------------------------------
1338 // Print special per-node info
1339 void BoolTest::dump_on(outputStream *st) const {
1340   const char *msg[] = {"eq","gt","of","lt","ne","le","nof","ge"};
1341   st->print("%s", msg[_test]);
1342 }
1343 
1344 // Returns the logical AND of two tests (or 'never' if both tests can never be true).
1345 // For example, a test for 'le' followed by a test for 'lt' is equivalent with 'lt'.
1346 BoolTest::mask BoolTest::merge(BoolTest other) const {
1347   const mask res[illegal+1][illegal+1] = {
1348     // eq,      gt,      of,      lt,      ne,      le,      nof,     ge,      never,   illegal
1349       {eq,      never,   illegal, never,   never,   eq,      illegal, eq,      never,   illegal},  // eq
1350       {never,   gt,      illegal, never,   gt,      never,   illegal, gt,      never,   illegal},  // gt
1351       {illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, never,   illegal},  // of
1352       {never,   never,   illegal, lt,      lt,      lt,      illegal, never,   never,   illegal},  // lt
1353       {never,   gt,      illegal, lt,      ne,      lt,      illegal, gt,      never,   illegal},  // ne
1354       {eq,      never,   illegal, lt,      lt,      le,      illegal, eq,      never,   illegal},  // le
1355       {illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, never,   illegal},  // nof
1356       {eq,      gt,      illegal, never,   gt,      eq,      illegal, ge,      never,   illegal},  // ge
1357       {never,   never,   never,   never,   never,   never,   never,   never,   never,   illegal},  // never
1358       {illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal, illegal}}; // illegal
1359   return res[_test][other._test];
1360 }
1361 
1362 //=============================================================================
1363 uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); }
1364 uint BoolNode::size_of() const { return sizeof(BoolNode); }
1365 
1366 //------------------------------operator==-------------------------------------
1367 bool BoolNode::cmp( const Node &n ) const {
1368   const BoolNode *b = (const BoolNode *)&n; // Cast up
1369   return (_test._test == b->_test._test);
1370 }
1371 
1372 //-------------------------------make_predicate--------------------------------
1373 Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
1374   if (test_value->is_Con())   return test_value;
1375   if (test_value->is_Bool())  return test_value;
1376   if (test_value->is_CMove() &&
1377       test_value->in(CMoveNode::Condition)->is_Bool()) {
1378     BoolNode*   bol   = test_value->in(CMoveNode::Condition)->as_Bool();
1379     const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
1380     const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
1381     if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
1382       return bol;
1383     } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
1384       return phase->transform( bol->negate(phase) );
1385     }
1386     // Else fall through.  The CMove gets in the way of the test.
1387     // It should be the case that make_predicate(bol->as_int_value()) == bol.
1388   }
1389   Node* cmp = new CmpINode(test_value, phase->intcon(0));
1390   cmp = phase->transform(cmp);
1391   Node* bol = new BoolNode(cmp, BoolTest::ne);
1392   return phase->transform(bol);
1393 }
1394 
1395 //--------------------------------as_int_value---------------------------------
1396 Node* BoolNode::as_int_value(PhaseGVN* phase) {
1397   // Inverse to make_predicate.  The CMove probably boils down to a Conv2B.
1398   Node* cmov = CMoveNode::make(NULL, this,
1399                                phase->intcon(0), phase->intcon(1),
1400                                TypeInt::BOOL);
1401   return phase->transform(cmov);
1402 }
1403 
1404 //----------------------------------negate-------------------------------------
1405 BoolNode* BoolNode::negate(PhaseGVN* phase) {
1406   return new BoolNode(in(1), _test.negate());
1407 }
1408 
1409 // Change "bool eq/ne (cmp (add/sub A B) C)" into false/true if add/sub
1410 // overflows and we can prove that C is not in the two resulting ranges.
1411 // This optimization is similar to the one performed by CmpUNode::Value().
1412 Node* BoolNode::fold_cmpI(PhaseGVN* phase, SubNode* cmp, Node* cmp1, int cmp_op,
1413                           int cmp1_op, const TypeInt* cmp2_type) {
1414   // Only optimize eq/ne integer comparison of add/sub
1415   if((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1416      (cmp_op == Op_CmpI) && (cmp1_op == Op_AddI || cmp1_op == Op_SubI)) {
1417     // Skip cases were inputs of add/sub are not integers or of bottom type
1418     const TypeInt* r0 = phase->type(cmp1->in(1))->isa_int();
1419     const TypeInt* r1 = phase->type(cmp1->in(2))->isa_int();
1420     if ((r0 != NULL) && (r0 != TypeInt::INT) &&
1421         (r1 != NULL) && (r1 != TypeInt::INT) &&
1422         (cmp2_type != TypeInt::INT)) {
1423       // Compute exact (long) type range of add/sub result
1424       jlong lo_long = r0->_lo;
1425       jlong hi_long = r0->_hi;
1426       if (cmp1_op == Op_AddI) {
1427         lo_long += r1->_lo;
1428         hi_long += r1->_hi;
1429       } else {
1430         lo_long -= r1->_hi;
1431         hi_long -= r1->_lo;
1432       }
1433       // Check for over-/underflow by casting to integer
1434       int lo_int = (int)lo_long;
1435       int hi_int = (int)hi_long;
1436       bool underflow = lo_long != (jlong)lo_int;
1437       bool overflow  = hi_long != (jlong)hi_int;
1438       if ((underflow != overflow) && (hi_int < lo_int)) {
1439         // Overflow on one boundary, compute resulting type ranges:
1440         // tr1 [MIN_INT, hi_int] and tr2 [lo_int, MAX_INT]
1441         int w = MAX2(r0->_widen, r1->_widen); // _widen does not matter here
1442         const TypeInt* tr1 = TypeInt::make(min_jint, hi_int, w);
1443         const TypeInt* tr2 = TypeInt::make(lo_int, max_jint, w);
1444         // Compare second input of cmp to both type ranges
1445         const Type* sub_tr1 = cmp->sub(tr1, cmp2_type);
1446         const Type* sub_tr2 = cmp->sub(tr2, cmp2_type);
1447         if (sub_tr1 == TypeInt::CC_LT && sub_tr2 == TypeInt::CC_GT) {
1448           // The result of the add/sub will never equal cmp2. Replace BoolNode
1449           // by false (0) if it tests for equality and by true (1) otherwise.
1450           return ConINode::make((_test._test == BoolTest::eq) ? 0 : 1);
1451         }
1452       }
1453     }
1454   }
1455   return NULL;
1456 }
1457 
1458 static bool is_counted_loop_cmp(Node *cmp) {
1459   Node *n = cmp->in(1)->in(1);
1460   return n != NULL &&
1461          n->is_Phi() &&
1462          n->in(0) != NULL &&
1463          n->in(0)->is_CountedLoop() &&
1464          n->in(0)->as_CountedLoop()->phi() == n;
1465 }
1466 
1467 //------------------------------Ideal------------------------------------------
1468 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1469   // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
1470   // This moves the constant to the right.  Helps value-numbering.
1471   Node *cmp = in(1);
1472   if( !cmp->is_Sub() ) return NULL;
1473   int cop = cmp->Opcode();
1474   if( cop == Op_FastLock || cop == Op_FastUnlock || cmp->is_SubTypeCheck()) return NULL;
1475   Node *cmp1 = cmp->in(1);
1476   Node *cmp2 = cmp->in(2);
1477   if( !cmp1 ) return NULL;
1478 
1479   if (_test._test == BoolTest::overflow || _test._test == BoolTest::no_overflow) {
1480     return NULL;
1481   }
1482 
1483   // Constant on left?
1484   Node *con = cmp1;
1485   uint op2 = cmp2->Opcode();
1486   // Move constants to the right of compare's to canonicalize.
1487   // Do not muck with Opaque1 nodes, as this indicates a loop
1488   // guard that cannot change shape.
1489   if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 &&
1490       // Because of NaN's, CmpD and CmpF are not commutative
1491       cop != Op_CmpD && cop != Op_CmpF &&
1492       // Protect against swapping inputs to a compare when it is used by a
1493       // counted loop exit, which requires maintaining the loop-limit as in(2)
1494       !is_counted_loop_exit_test() ) {
1495     // Ok, commute the constant to the right of the cmp node.
1496     // Clone the Node, getting a new Node of the same class
1497     cmp = cmp->clone();
1498     // Swap inputs to the clone
1499     cmp->swap_edges(1, 2);
1500     cmp = phase->transform( cmp );
1501     return new BoolNode( cmp, _test.commute() );
1502   }
1503 
1504   // Change "bool eq/ne (cmp (and X 16) 16)" into "bool ne/eq (cmp (and X 16) 0)".
1505   if (cop == Op_CmpI &&
1506       (_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1507       cmp1->Opcode() == Op_AndI && cmp2->Opcode() == Op_ConI &&
1508       cmp1->in(2)->Opcode() == Op_ConI) {
1509     const TypeInt *t12 = phase->type(cmp2)->isa_int();
1510     const TypeInt *t112 = phase->type(cmp1->in(2))->isa_int();
1511     if (t12 && t12->is_con() && t112 && t112->is_con() &&
1512         t12->get_con() == t112->get_con() && is_power_of_2(t12->get_con())) {
1513       Node *ncmp = phase->transform(new CmpINode(cmp1, phase->intcon(0)));
1514       return new BoolNode(ncmp, _test.negate());
1515     }
1516   }
1517 
1518   // Same for long type: change "bool eq/ne (cmp (and X 16) 16)" into "bool ne/eq (cmp (and X 16) 0)".
1519   if (cop == Op_CmpL &&
1520       (_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1521       cmp1->Opcode() == Op_AndL && cmp2->Opcode() == Op_ConL &&
1522       cmp1->in(2)->Opcode() == Op_ConL) {
1523     const TypeLong *t12 = phase->type(cmp2)->isa_long();
1524     const TypeLong *t112 = phase->type(cmp1->in(2))->isa_long();
1525     if (t12 && t12->is_con() && t112 && t112->is_con() &&
1526         t12->get_con() == t112->get_con() && is_power_of_2(t12->get_con())) {
1527       Node *ncmp = phase->transform(new CmpLNode(cmp1, phase->longcon(0)));
1528       return new BoolNode(ncmp, _test.negate());
1529     }
1530   }
1531 
1532   // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
1533   // The XOR-1 is an idiom used to flip the sense of a bool.  We flip the
1534   // test instead.
1535   int cmp1_op = cmp1->Opcode();
1536   const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
1537   if (cmp2_type == NULL)  return NULL;
1538   Node* j_xor = cmp1;
1539   if( cmp2_type == TypeInt::ZERO &&
1540       cmp1_op == Op_XorI &&
1541       j_xor->in(1) != j_xor &&          // An xor of itself is dead
1542       phase->type( j_xor->in(1) ) == TypeInt::BOOL &&
1543       phase->type( j_xor->in(2) ) == TypeInt::ONE &&
1544       (_test._test == BoolTest::eq ||
1545        _test._test == BoolTest::ne) ) {
1546     Node *ncmp = phase->transform(new CmpINode(j_xor->in(1),cmp2));
1547     return new BoolNode( ncmp, _test.negate() );
1548   }
1549 
1550   // Change ((x & m) u<= m) or ((m & x) u<= m) to always true
1551   // Same with ((x & m) u< m+1) and ((m & x) u< m+1)
1552   if (cop == Op_CmpU &&
1553       cmp1_op == Op_AndI) {
1554     Node* bound = NULL;
1555     if (_test._test == BoolTest::le) {
1556       bound = cmp2;
1557     } else if (_test._test == BoolTest::lt &&
1558                cmp2->Opcode() == Op_AddI &&
1559                cmp2->in(2)->find_int_con(0) == 1) {
1560       bound = cmp2->in(1);
1561     }
1562     if (cmp1->in(2) == bound || cmp1->in(1) == bound) {
1563       return ConINode::make(1);
1564     }
1565   }
1566 
1567   // Change ((x & (m - 1)) u< m) into (m > 0)
1568   // This is the off-by-one variant of the above
1569   if (cop == Op_CmpU &&
1570       _test._test == BoolTest::lt &&
1571       cmp1_op == Op_AndI) {
1572     Node* l = cmp1->in(1);
1573     Node* r = cmp1->in(2);
1574     for (int repeat = 0; repeat < 2; repeat++) {
1575       bool match = r->Opcode() == Op_AddI && r->in(2)->find_int_con(0) == -1 &&
1576                    r->in(1) == cmp2;
1577       if (match) {
1578         // arraylength known to be non-negative, so a (arraylength != 0) is sufficient,
1579         // but to be compatible with the array range check pattern, use (arraylength u> 0)
1580         Node* ncmp = cmp2->Opcode() == Op_LoadRange
1581                      ? phase->transform(new CmpUNode(cmp2, phase->intcon(0)))
1582                      : phase->transform(new CmpINode(cmp2, phase->intcon(0)));
1583         return new BoolNode(ncmp, BoolTest::gt);
1584       } else {
1585         // commute and try again
1586         l = cmp1->in(2);
1587         r = cmp1->in(1);
1588       }
1589     }
1590   }
1591 
1592   // Change x u< 1 or x u<= 0 to x == 0
1593   if (cop == Op_CmpU &&
1594       cmp1_op != Op_LoadRange &&
1595       ((_test._test == BoolTest::lt &&
1596         cmp2->find_int_con(-1) == 1) ||
1597        (_test._test == BoolTest::le &&
1598         cmp2->find_int_con(-1) == 0))) {
1599     Node* ncmp = phase->transform(new CmpINode(cmp1, phase->intcon(0)));
1600     return new BoolNode(ncmp, BoolTest::eq);
1601   }
1602 
1603   // Change (arraylength <= 0) or (arraylength == 0)
1604   //   into (arraylength u<= 0)
1605   // Also change (arraylength != 0) into (arraylength u> 0)
1606   // The latter version matches the code pattern generated for
1607   // array range checks, which will more likely be optimized later.
1608   if (cop == Op_CmpI &&
1609       cmp1_op == Op_LoadRange &&
1610       cmp2->find_int_con(-1) == 0) {
1611     if (_test._test == BoolTest::le || _test._test == BoolTest::eq) {
1612       Node* ncmp = phase->transform(new CmpUNode(cmp1, cmp2));
1613       return new BoolNode(ncmp, BoolTest::le);
1614     } else if (_test._test == BoolTest::ne) {
1615       Node* ncmp = phase->transform(new CmpUNode(cmp1, cmp2));
1616       return new BoolNode(ncmp, BoolTest::gt);
1617     }
1618   }
1619 
1620   // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
1621   // This is a standard idiom for branching on a boolean value.
1622   Node *c2b = cmp1;
1623   if( cmp2_type == TypeInt::ZERO &&
1624       cmp1_op == Op_Conv2B &&
1625       (_test._test == BoolTest::eq ||
1626        _test._test == BoolTest::ne) ) {
1627     Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
1628        ? (Node*)new CmpINode(c2b->in(1),cmp2)
1629        : (Node*)new CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
1630     );
1631     return new BoolNode( ncmp, _test._test );
1632   }
1633 
1634   // Comparing a SubI against a zero is equal to comparing the SubI
1635   // arguments directly.  This only works for eq and ne comparisons
1636   // due to possible integer overflow.
1637   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1638         (cop == Op_CmpI) &&
1639         (cmp1_op == Op_SubI) &&
1640         ( cmp2_type == TypeInt::ZERO ) ) {
1641     Node *ncmp = phase->transform( new CmpINode(cmp1->in(1),cmp1->in(2)));
1642     return new BoolNode( ncmp, _test._test );
1643   }
1644 
1645   // Same as above but with and AddI of a constant
1646   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1647       cop == Op_CmpI &&
1648       cmp1_op == Op_AddI &&
1649       cmp1->in(2) != NULL &&
1650       phase->type(cmp1->in(2))->isa_int() &&
1651       phase->type(cmp1->in(2))->is_int()->is_con() &&
1652       cmp2_type == TypeInt::ZERO &&
1653       !is_counted_loop_cmp(cmp) // modifying the exit test of a counted loop messes the counted loop shape
1654       ) {
1655     const TypeInt* cmp1_in2 = phase->type(cmp1->in(2))->is_int();
1656     Node *ncmp = phase->transform( new CmpINode(cmp1->in(1),phase->intcon(-cmp1_in2->_hi)));
1657     return new BoolNode( ncmp, _test._test );
1658   }
1659 
1660   // Change "bool eq/ne (cmp (phi (X -X) 0))" into "bool eq/ne (cmp X 0)"
1661   // since zero check of conditional negation of an integer is equal to
1662   // zero check of the integer directly.
1663   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1664       (cop == Op_CmpI) &&
1665       (cmp2_type == TypeInt::ZERO) &&
1666       (cmp1_op == Op_Phi)) {
1667     // There should be a diamond phi with true path at index 1 or 2
1668     PhiNode *phi = cmp1->as_Phi();
1669     int idx_true = phi->is_diamond_phi();
1670     if (idx_true != 0) {
1671       // True input is in(idx_true) while false input is in(3 - idx_true)
1672       Node *tin = phi->in(idx_true);
1673       Node *fin = phi->in(3 - idx_true);
1674       if ((tin->Opcode() == Op_SubI) &&
1675           (phase->type(tin->in(1)) == TypeInt::ZERO) &&
1676           (tin->in(2) == fin)) {
1677         // Found conditional negation at true path, create a new CmpINode without that
1678         Node *ncmp = phase->transform(new CmpINode(fin, cmp2));
1679         return new BoolNode(ncmp, _test._test);
1680       }
1681       if ((fin->Opcode() == Op_SubI) &&
1682           (phase->type(fin->in(1)) == TypeInt::ZERO) &&
1683           (fin->in(2) == tin)) {
1684         // Found conditional negation at false path, create a new CmpINode without that
1685         Node *ncmp = phase->transform(new CmpINode(tin, cmp2));
1686         return new BoolNode(ncmp, _test._test);
1687       }
1688     }
1689   }
1690 
1691   // Change (-A vs 0) into (A vs 0) by commuting the test.  Disallow in the
1692   // most general case because negating 0x80000000 does nothing.  Needed for
1693   // the CmpF3/SubI/CmpI idiom.
1694   if( cop == Op_CmpI &&
1695       cmp1_op == Op_SubI &&
1696       cmp2_type == TypeInt::ZERO &&
1697       phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
1698       phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
1699     Node *ncmp = phase->transform( new CmpINode(cmp1->in(2),cmp2));
1700     return new BoolNode( ncmp, _test.commute() );
1701   }
1702 
1703   // Try to optimize signed integer comparison
1704   return fold_cmpI(phase, cmp->as_Sub(), cmp1, cop, cmp1_op, cmp2_type);
1705 
1706   //  The transformation below is not valid for either signed or unsigned
1707   //  comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
1708   //  This transformation can be resurrected when we are able to
1709   //  make inferences about the range of values being subtracted from
1710   //  (or added to) relative to the wraparound point.
1711   //
1712   //    // Remove +/-1's if possible.
1713   //    // "X <= Y-1" becomes "X <  Y"
1714   //    // "X+1 <= Y" becomes "X <  Y"
1715   //    // "X <  Y+1" becomes "X <= Y"
1716   //    // "X-1 <  Y" becomes "X <= Y"
1717   //    // Do not this to compares off of the counted-loop-end.  These guys are
1718   //    // checking the trip counter and they want to use the post-incremented
1719   //    // counter.  If they use the PRE-incremented counter, then the counter has
1720   //    // to be incremented in a private block on a loop backedge.
1721   //    if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
1722   //      return NULL;
1723   //  #ifndef PRODUCT
1724   //    // Do not do this in a wash GVN pass during verification.
1725   //    // Gets triggered by too many simple optimizations to be bothered with
1726   //    // re-trying it again and again.
1727   //    if( !phase->allow_progress() ) return NULL;
1728   //  #endif
1729   //    // Not valid for unsigned compare because of corner cases in involving zero.
1730   //    // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an
1731   //    // exception in case X is 0 (because 0-1 turns into 4billion unsigned but
1732   //    // "0 <=u Y" is always true).
1733   //    if( cmp->Opcode() == Op_CmpU ) return NULL;
1734   //    int cmp2_op = cmp2->Opcode();
1735   //    if( _test._test == BoolTest::le ) {
1736   //      if( cmp1_op == Op_AddI &&
1737   //          phase->type( cmp1->in(2) ) == TypeInt::ONE )
1738   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt );
1739   //      else if( cmp2_op == Op_AddI &&
1740   //         phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 )
1741   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt );
1742   //    } else if( _test._test == BoolTest::lt ) {
1743   //      if( cmp1_op == Op_AddI &&
1744   //          phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 )
1745   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le );
1746   //      else if( cmp2_op == Op_AddI &&
1747   //         phase->type( cmp2->in(2) ) == TypeInt::ONE )
1748   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le );
1749   //    }
1750 }
1751 
1752 //------------------------------Value------------------------------------------
1753 // Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
1754 // based on local information.   If the input is constant, do it.
1755 const Type* BoolNode::Value(PhaseGVN* phase) const {
1756   return _test.cc2logical( phase->type( in(1) ) );
1757 }
1758 
1759 #ifndef PRODUCT
1760 //------------------------------dump_spec--------------------------------------
1761 // Dump special per-node info
1762 void BoolNode::dump_spec(outputStream *st) const {
1763   st->print("[");
1764   _test.dump_on(st);
1765   st->print("]");
1766 }
1767 
1768 //-------------------------------related---------------------------------------
1769 // A BoolNode's related nodes are all of its data inputs, and all of its
1770 // outputs until control nodes are hit, which are included. In compact
1771 // representation, inputs till level 3 and immediate outputs are included.
1772 void BoolNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {
1773   if (compact) {
1774     this->collect_nodes(in_rel, 3, false, true);
1775     this->collect_nodes(out_rel, -1, false, false);
1776   } else {
1777     this->collect_nodes_in_all_data(in_rel, false);
1778     this->collect_nodes_out_all_ctrl_boundary(out_rel);
1779   }
1780 }
1781 #endif
1782 
1783 //----------------------is_counted_loop_exit_test------------------------------
1784 // Returns true if node is used by a counted loop node.
1785 bool BoolNode::is_counted_loop_exit_test() {
1786   for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
1787     Node* use = fast_out(i);
1788     if (use->is_CountedLoopEnd()) {
1789       return true;
1790     }
1791   }
1792   return false;
1793 }
1794 
1795 //=============================================================================
1796 //------------------------------Value------------------------------------------
1797 // Compute sqrt
1798 const Type* SqrtDNode::Value(PhaseGVN* phase) const {
1799   const Type *t1 = phase->type( in(1) );
1800   if( t1 == Type::TOP ) return Type::TOP;
1801   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1802   double d = t1->getd();
1803   if( d < 0.0 ) return Type::DOUBLE;
1804   return TypeD::make( sqrt( d ) );
1805 }
1806 
1807 const Type* SqrtFNode::Value(PhaseGVN* phase) const {
1808   const Type *t1 = phase->type( in(1) );
1809   if( t1 == Type::TOP ) return Type::TOP;
1810   if( t1->base() != Type::FloatCon ) return Type::FLOAT;
1811   float f = t1->getf();
1812   if( f < 0.0f ) return Type::FLOAT;
1813   return TypeF::make( (float)sqrt( (double)f ) );
1814 }