1 /*
   2  * Copyright (c) 1997, 2022, 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 "memory/allocation.inline.hpp"
  27 #include "opto/addnode.hpp"
  28 #include "opto/castnode.hpp"
  29 #include "opto/cfgnode.hpp"
  30 #include "opto/connode.hpp"
  31 #include "opto/machnode.hpp"
  32 #include "opto/movenode.hpp"
  33 #include "opto/mulnode.hpp"
  34 #include "opto/phaseX.hpp"
  35 #include "opto/subnode.hpp"
  36 
  37 // Portions of code courtesy of Clifford Click
  38 
  39 // Classic Add functionality.  This covers all the usual 'add' behaviors for
  40 // an algebraic ring.  Add-integer, add-float, add-double, and binary-or are
  41 // all inherited from this class.  The various identity values are supplied
  42 // by virtual functions.
  43 
  44 
  45 //=============================================================================
  46 //------------------------------hash-------------------------------------------
  47 // Hash function over AddNodes.  Needs to be commutative; i.e., I swap
  48 // (commute) inputs to AddNodes willy-nilly so the hash function must return
  49 // the same value in the presence of edge swapping.
  50 uint AddNode::hash() const {
  51   return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();
  52 }
  53 
  54 //------------------------------Identity---------------------------------------
  55 // If either input is a constant 0, return the other input.
  56 Node* AddNode::Identity(PhaseGVN* phase) {
  57   const Type *zero = add_id();  // The additive identity
  58   if( phase->type( in(1) )->higher_equal( zero ) ) return in(2);
  59   if( phase->type( in(2) )->higher_equal( zero ) ) return in(1);
  60   return this;
  61 }
  62 
  63 //------------------------------commute----------------------------------------
  64 // Commute operands to move loads and constants to the right.
  65 static bool commute(PhaseGVN* phase, Node* add) {
  66   Node *in1 = add->in(1);
  67   Node *in2 = add->in(2);
  68 
  69   // convert "max(a,b) + min(a,b)" into "a+b".
  70   if ((in1->Opcode() == add->as_Add()->max_opcode() && in2->Opcode() == add->as_Add()->min_opcode())
  71       || (in1->Opcode() == add->as_Add()->min_opcode() && in2->Opcode() == add->as_Add()->max_opcode())) {
  72     Node *in11 = in1->in(1);
  73     Node *in12 = in1->in(2);
  74 
  75     Node *in21 = in2->in(1);
  76     Node *in22 = in2->in(2);
  77 
  78     if ((in11 == in21 && in12 == in22) ||
  79         (in11 == in22 && in12 == in21)) {
  80       add->set_req_X(1, in11, phase);
  81       add->set_req_X(2, in12, phase);
  82       return true;
  83     }
  84   }
  85 
  86   bool con_left = phase->type(in1)->singleton();
  87   bool con_right = phase->type(in2)->singleton();
  88 
  89   // Convert "1+x" into "x+1".
  90   // Right is a constant; leave it
  91   if( con_right ) return false;
  92   // Left is a constant; move it right.
  93   if( con_left ) {
  94     add->swap_edges(1, 2);
  95     return true;
  96   }
  97 
  98   // Convert "Load+x" into "x+Load".
  99   // Now check for loads
 100   if (in2->is_Load()) {
 101     if (!in1->is_Load()) {
 102       // already x+Load to return
 103       return false;
 104     }
 105     // both are loads, so fall through to sort inputs by idx
 106   } else if( in1->is_Load() ) {
 107     // Left is a Load and Right is not; move it right.
 108     add->swap_edges(1, 2);
 109     return true;
 110   }
 111 
 112   PhiNode *phi;
 113   // Check for tight loop increments: Loop-phi of Add of loop-phi
 114   if (in1->is_Phi() && (phi = in1->as_Phi()) && phi->region()->is_Loop() && phi->in(2) == add)
 115     return false;
 116   if (in2->is_Phi() && (phi = in2->as_Phi()) && phi->region()->is_Loop() && phi->in(2) == add) {
 117     add->swap_edges(1, 2);
 118     return true;
 119   }
 120 
 121   // Otherwise, sort inputs (commutativity) to help value numbering.
 122   if( in1->_idx > in2->_idx ) {
 123     add->swap_edges(1, 2);
 124     return true;
 125   }
 126   return false;
 127 }
 128 
 129 //------------------------------Idealize---------------------------------------
 130 // If we get here, we assume we are associative!
 131 Node *AddNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 132   const Type *t1 = phase->type(in(1));
 133   const Type *t2 = phase->type(in(2));
 134   bool con_left  = t1->singleton();
 135   bool con_right = t2->singleton();
 136 
 137   // Check for commutative operation desired
 138   if (commute(phase, this)) return this;
 139 
 140   AddNode *progress = NULL;             // Progress flag
 141 
 142   // Convert "(x+1)+2" into "x+(1+2)".  If the right input is a
 143   // constant, and the left input is an add of a constant, flatten the
 144   // expression tree.
 145   Node *add1 = in(1);
 146   Node *add2 = in(2);
 147   int add1_op = add1->Opcode();
 148   int this_op = Opcode();
 149   if (con_right && t2 != Type::TOP && // Right input is a constant?
 150       add1_op == this_op) { // Left input is an Add?
 151 
 152     // Type of left _in right input
 153     const Type *t12 = phase->type(add1->in(2));
 154     if (t12->singleton() && t12 != Type::TOP) { // Left input is an add of a constant?
 155       // Check for rare case of closed data cycle which can happen inside
 156       // unreachable loops. In these cases the computation is undefined.
 157 #ifdef ASSERT
 158       Node *add11    = add1->in(1);
 159       int   add11_op = add11->Opcode();
 160       if ((add1 == add1->in(1))
 161           || (add11_op == this_op && add11->in(1) == add1)) {
 162         assert(false, "dead loop in AddNode::Ideal");
 163       }
 164 #endif
 165       // The Add of the flattened expression
 166       Node *x1 = add1->in(1);
 167       Node *x2 = phase->makecon(add1->as_Add()->add_ring(t2, t12));
 168       set_req_X(2, x2, phase);
 169       set_req_X(1, x1, phase);
 170       progress = this;            // Made progress
 171       add1 = in(1);
 172       add1_op = add1->Opcode();
 173     }
 174   }
 175 
 176   // Convert "(x+1)+y" into "(x+y)+1".  Push constants down the expression tree.
 177   if (add1_op == this_op && !con_right) {
 178     Node *a12 = add1->in(2);
 179     const Type *t12 = phase->type( a12 );
 180     if (t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) &&
 181         !(add1->in(1)->is_Phi() && (add1->in(1)->as_Phi()->is_tripcount(T_INT) || add1->in(1)->as_Phi()->is_tripcount(T_LONG)))) {
 182       assert(add1->in(1) != this, "dead loop in AddNode::Ideal");
 183       add2 = add1->clone();
 184       add2->set_req(2, in(2));
 185       add2 = phase->transform(add2);
 186       set_req_X(1, add2, phase);
 187       set_req_X(2, a12, phase);
 188       progress = this;
 189       add2 = a12;
 190     }
 191   }
 192 
 193   // Convert "x+(y+1)" into "(x+y)+1".  Push constants down the expression tree.
 194   int add2_op = add2->Opcode();
 195   if (add2_op == this_op && !con_left) {
 196     Node *a22 = add2->in(2);
 197     const Type *t22 = phase->type( a22 );
 198     if (t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) &&
 199         !(add2->in(1)->is_Phi() && (add2->in(1)->as_Phi()->is_tripcount(T_INT) || add2->in(1)->as_Phi()->is_tripcount(T_LONG)))) {
 200       assert(add2->in(1) != this, "dead loop in AddNode::Ideal");
 201       Node *addx = add2->clone();
 202       addx->set_req(1, in(1));
 203       addx->set_req(2, add2->in(1));
 204       addx = phase->transform(addx);
 205       set_req_X(1, addx, phase);
 206       set_req_X(2, a22, phase);
 207       progress = this;
 208     }
 209   }
 210 
 211   return progress;
 212 }
 213 
 214 //------------------------------Value-----------------------------------------
 215 // An add node sums it's two _in.  If one input is an RSD, we must mixin
 216 // the other input's symbols.
 217 const Type* AddNode::Value(PhaseGVN* phase) const {
 218   // Either input is TOP ==> the result is TOP
 219   const Type *t1 = phase->type( in(1) );
 220   const Type *t2 = phase->type( in(2) );
 221   if( t1 == Type::TOP ) return Type::TOP;
 222   if( t2 == Type::TOP ) return Type::TOP;
 223 
 224   // Either input is BOTTOM ==> the result is the local BOTTOM
 225   const Type *bot = bottom_type();
 226   if( (t1 == bot) || (t2 == bot) ||
 227       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 228     return bot;
 229 
 230   // Check for an addition involving the additive identity
 231   const Type *tadd = add_of_identity( t1, t2 );
 232   if( tadd ) return tadd;
 233 
 234   return add_ring(t1,t2);               // Local flavor of type addition
 235 }
 236 
 237 //------------------------------add_identity-----------------------------------
 238 // Check for addition of the identity
 239 const Type *AddNode::add_of_identity( const Type *t1, const Type *t2 ) const {
 240   const Type *zero = add_id();  // The additive identity
 241   if( t1->higher_equal( zero ) ) return t2;
 242   if( t2->higher_equal( zero ) ) return t1;
 243 
 244   return NULL;
 245 }
 246 
 247 AddNode* AddNode::make(Node* in1, Node* in2, BasicType bt) {
 248   switch (bt) {
 249     case T_INT:
 250       return new AddINode(in1, in2);
 251     case T_LONG:
 252       return new AddLNode(in1, in2);
 253     default:
 254       fatal("Not implemented for %s", type2name(bt));
 255   }
 256   return NULL;
 257 }
 258 
 259 //=============================================================================
 260 //------------------------------Idealize---------------------------------------
 261 Node* AddNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) {
 262   Node* in1 = in(1);
 263   Node* in2 = in(2);
 264   int op1 = in1->Opcode();
 265   int op2 = in2->Opcode();
 266   // Fold (con1-x)+con2 into (con1+con2)-x
 267   if (op1 == Op_Add(bt) && op2 == Op_Sub(bt)) {
 268     // Swap edges to try optimizations below
 269     in1 = in2;
 270     in2 = in(1);
 271     op1 = op2;
 272     op2 = in2->Opcode();
 273   }
 274   if (op1 == Op_Sub(bt)) {
 275     const Type* t_sub1 = phase->type(in1->in(1));
 276     const Type* t_2    = phase->type(in2       );
 277     if (t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP) {
 278       return SubNode::make(phase->makecon(add_ring(t_sub1, t_2)), in1->in(2), bt);
 279     }
 280     // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
 281     if (op2 == Op_Sub(bt)) {
 282       // Check for dead cycle: d = (a-b)+(c-d)
 283       assert( in1->in(2) != this && in2->in(2) != this,
 284               "dead loop in AddINode::Ideal" );
 285       Node* sub = SubNode::make(NULL, NULL, bt);
 286       sub->init_req(1, phase->transform(AddNode::make(in1->in(1), in2->in(1), bt)));
 287       sub->init_req(2, phase->transform(AddNode::make(in1->in(2), in2->in(2), bt)));
 288       return sub;
 289     }
 290     // Convert "(a-b)+(b+c)" into "(a+c)"
 291     if (op2 == Op_Add(bt) && in1->in(2) == in2->in(1)) {
 292       assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal/AddLNode::Ideal");
 293       return AddNode::make(in1->in(1), in2->in(2), bt);
 294     }
 295     // Convert "(a-b)+(c+b)" into "(a+c)"
 296     if (op2 == Op_Add(bt) && in1->in(2) == in2->in(2)) {
 297       assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal/AddLNode::Ideal");
 298       return AddNode::make(in1->in(1), in2->in(1), bt);
 299     }
 300   }
 301 
 302   // Convert (con - y) + x into "(x - y) + con"
 303   if (op1 == Op_Sub(bt) && in1->in(1)->Opcode() == Op_ConIL(bt)
 304       && in1 != in1->in(2) && !(in1->in(2)->is_Phi() && in1->in(2)->as_Phi()->is_tripcount(bt))) {
 305     return AddNode::make(phase->transform(SubNode::make(in2, in1->in(2), bt)), in1->in(1), bt);
 306   }
 307 
 308   // Convert x + (con - y) into "(x - y) + con"
 309   if (op2 == Op_Sub(bt) && in2->in(1)->Opcode() == Op_ConIL(bt)
 310       && in2 != in2->in(2) && !(in2->in(2)->is_Phi() && in2->in(2)->as_Phi()->is_tripcount(bt))) {
 311     return AddNode::make(phase->transform(SubNode::make(in1, in2->in(2), bt)), in2->in(1), bt);
 312   }
 313 
 314   // Associative
 315   if (op1 == Op_Mul(bt) && op2 == Op_Mul(bt)) {
 316     Node* add_in1 = NULL;
 317     Node* add_in2 = NULL;
 318     Node* mul_in = NULL;
 319 
 320     if (in1->in(1) == in2->in(1)) {
 321       // Convert "a*b+a*c into a*(b+c)
 322       add_in1 = in1->in(2);
 323       add_in2 = in2->in(2);
 324       mul_in = in1->in(1);
 325     } else if (in1->in(2) == in2->in(1)) {
 326       // Convert a*b+b*c into b*(a+c)
 327       add_in1 = in1->in(1);
 328       add_in2 = in2->in(2);
 329       mul_in = in1->in(2);
 330     } else if (in1->in(2) == in2->in(2)) {
 331       // Convert a*c+b*c into (a+b)*c
 332       add_in1 = in1->in(1);
 333       add_in2 = in2->in(1);
 334       mul_in = in1->in(2);
 335     } else if (in1->in(1) == in2->in(2)) {
 336       // Convert a*b+c*a into a*(b+c)
 337       add_in1 = in1->in(2);
 338       add_in2 = in2->in(1);
 339       mul_in = in1->in(1);
 340     }
 341 
 342     if (mul_in != NULL) {
 343       Node* add = phase->transform(AddNode::make(add_in1, add_in2, bt));
 344       return MulNode::make(mul_in, add, bt);
 345     }
 346   }
 347 
 348   // Convert (x >>> rshift) + (x << lshift) into RotateRight(x, rshift)
 349   if (Matcher::match_rule_supported(Op_RotateRight) &&
 350       ((op1 == Op_URShift(bt) && op2 == Op_LShift(bt)) || (op1 == Op_LShift(bt) && op2 == Op_URShift(bt))) &&
 351       in1->in(1) != NULL && in1->in(1) == in2->in(1)) {
 352     Node* rshift = op1 == Op_URShift(bt) ? in1->in(2) : in2->in(2);
 353     Node* lshift = op1 == Op_URShift(bt) ? in2->in(2) : in1->in(2);
 354     if (rshift != NULL && lshift != NULL) {
 355       const TypeInt* rshift_t = phase->type(rshift)->isa_int();
 356       const TypeInt* lshift_t = phase->type(lshift)->isa_int();
 357       int bits = bt == T_INT ? 32 : 64;
 358       int mask = bt == T_INT ? 0x1F : 0x3F;
 359       if (lshift_t != NULL && lshift_t->is_con() &&
 360           rshift_t != NULL && rshift_t->is_con() &&
 361           ((lshift_t->get_con() & mask) == (bits - (rshift_t->get_con() & mask)))) {
 362         return new RotateRightNode(in1->in(1), phase->intcon(rshift_t->get_con() & mask), TypeInteger::bottom(bt));
 363       }
 364     }
 365   }
 366 
 367   return AddNode::Ideal(phase, can_reshape);
 368 }
 369 
 370 
 371 Node* AddINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 372   Node* in1 = in(1);
 373   Node* in2 = in(2);
 374   int op1 = in1->Opcode();
 375   int op2 = in2->Opcode();
 376 
 377   // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
 378   // Helps with array allocation math constant folding
 379   // See 4790063:
 380   // Unrestricted transformation is unsafe for some runtime values of 'x'
 381   // ( x ==  0, z == 1, y == -1 ) fails
 382   // ( x == -5, z == 1, y ==  1 ) fails
 383   // Transform works for small z and small negative y when the addition
 384   // (x + (y << z)) does not cross zero.
 385   // Implement support for negative y and (x >= -(y << z))
 386   // Have not observed cases where type information exists to support
 387   // positive y and (x <= -(y << z))
 388   if (op1 == Op_URShiftI && op2 == Op_ConI &&
 389       in1->in(2)->Opcode() == Op_ConI) {
 390     jint z = phase->type(in1->in(2))->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
 391     jint y = phase->type(in2)->is_int()->get_con();
 392 
 393     if (z < 5 && -5 < y && y < 0) {
 394       const Type* t_in11 = phase->type(in1->in(1));
 395       if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z))) {
 396         Node* a = phase->transform(new AddINode( in1->in(1), phase->intcon(y<<z)));
 397         return new URShiftINode(a, in1->in(2));
 398       }
 399     }
 400   }
 401 
 402   return AddNode::IdealIL(phase, can_reshape, T_INT);
 403 }
 404 
 405 
 406 //------------------------------Identity---------------------------------------
 407 // Fold (x-y)+y  OR  y+(x-y)  into  x
 408 Node* AddINode::Identity(PhaseGVN* phase) {
 409   if (in(1)->Opcode() == Op_SubI && in(1)->in(2) == in(2)) {
 410     return in(1)->in(1);
 411   } else if (in(2)->Opcode() == Op_SubI && in(2)->in(2) == in(1)) {
 412     return in(2)->in(1);
 413   }
 414   return AddNode::Identity(phase);
 415 }
 416 
 417 
 418 //------------------------------add_ring---------------------------------------
 419 // Supplied function returns the sum of the inputs.  Guaranteed never
 420 // to be passed a TOP or BOTTOM type, these are filtered out by
 421 // pre-check.
 422 const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {
 423   const TypeInt *r0 = t0->is_int(); // Handy access
 424   const TypeInt *r1 = t1->is_int();
 425   int lo = java_add(r0->_lo, r1->_lo);
 426   int hi = java_add(r0->_hi, r1->_hi);
 427   if( !(r0->is_con() && r1->is_con()) ) {
 428     // Not both constants, compute approximate result
 429     if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
 430       lo = min_jint; hi = max_jint; // Underflow on the low side
 431     }
 432     if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
 433       lo = min_jint; hi = max_jint; // Overflow on the high side
 434     }
 435     if( lo > hi ) {               // Handle overflow
 436       lo = min_jint; hi = max_jint;
 437     }
 438   } else {
 439     // both constants, compute precise result using 'lo' and 'hi'
 440     // Semantics define overflow and underflow for integer addition
 441     // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
 442   }
 443   return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
 444 }
 445 
 446 
 447 //=============================================================================
 448 //------------------------------Idealize---------------------------------------
 449 Node* AddLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 450   return AddNode::IdealIL(phase, can_reshape, T_LONG);
 451 }
 452 
 453 
 454 //------------------------------Identity---------------------------------------
 455 // Fold (x-y)+y  OR  y+(x-y)  into  x
 456 Node* AddLNode::Identity(PhaseGVN* phase) {
 457   if (in(1)->Opcode() == Op_SubL && in(1)->in(2) == in(2)) {
 458     return in(1)->in(1);
 459   } else if (in(2)->Opcode() == Op_SubL && in(2)->in(2) == in(1)) {
 460     return in(2)->in(1);
 461   }
 462   return AddNode::Identity(phase);
 463 }
 464 
 465 
 466 //------------------------------add_ring---------------------------------------
 467 // Supplied function returns the sum of the inputs.  Guaranteed never
 468 // to be passed a TOP or BOTTOM type, these are filtered out by
 469 // pre-check.
 470 const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {
 471   const TypeLong *r0 = t0->is_long(); // Handy access
 472   const TypeLong *r1 = t1->is_long();
 473   jlong lo = java_add(r0->_lo, r1->_lo);
 474   jlong hi = java_add(r0->_hi, r1->_hi);
 475   if( !(r0->is_con() && r1->is_con()) ) {
 476     // Not both constants, compute approximate result
 477     if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
 478       lo =min_jlong; hi = max_jlong; // Underflow on the low side
 479     }
 480     if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
 481       lo = min_jlong; hi = max_jlong; // Overflow on the high side
 482     }
 483     if( lo > hi ) {               // Handle overflow
 484       lo = min_jlong; hi = max_jlong;
 485     }
 486   } else {
 487     // both constants, compute precise result using 'lo' and 'hi'
 488     // Semantics define overflow and underflow for integer addition
 489     // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
 490   }
 491   return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
 492 }
 493 
 494 
 495 //=============================================================================
 496 //------------------------------add_of_identity--------------------------------
 497 // Check for addition of the identity
 498 const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {
 499   // x ADD 0  should return x unless 'x' is a -zero
 500   //
 501   // const Type *zero = add_id();     // The additive identity
 502   // jfloat f1 = t1->getf();
 503   // jfloat f2 = t2->getf();
 504   //
 505   // if( t1->higher_equal( zero ) ) return t2;
 506   // if( t2->higher_equal( zero ) ) return t1;
 507 
 508   return NULL;
 509 }
 510 
 511 //------------------------------add_ring---------------------------------------
 512 // Supplied function returns the sum of the inputs.
 513 // This also type-checks the inputs for sanity.  Guaranteed never to
 514 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 515 const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {
 516   // We must be adding 2 float constants.
 517   return TypeF::make( t0->getf() + t1->getf() );
 518 }
 519 
 520 //------------------------------Ideal------------------------------------------
 521 Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 522   // Floating point additions are not associative because of boundary conditions (infinity)
 523   return commute(phase, this) ? this : NULL;
 524 }
 525 
 526 
 527 //=============================================================================
 528 //------------------------------add_of_identity--------------------------------
 529 // Check for addition of the identity
 530 const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {
 531   // x ADD 0  should return x unless 'x' is a -zero
 532   //
 533   // const Type *zero = add_id();     // The additive identity
 534   // jfloat f1 = t1->getf();
 535   // jfloat f2 = t2->getf();
 536   //
 537   // if( t1->higher_equal( zero ) ) return t2;
 538   // if( t2->higher_equal( zero ) ) return t1;
 539 
 540   return NULL;
 541 }
 542 //------------------------------add_ring---------------------------------------
 543 // Supplied function returns the sum of the inputs.
 544 // This also type-checks the inputs for sanity.  Guaranteed never to
 545 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 546 const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {
 547   // We must be adding 2 double constants.
 548   return TypeD::make( t0->getd() + t1->getd() );
 549 }
 550 
 551 //------------------------------Ideal------------------------------------------
 552 Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 553   // Floating point additions are not associative because of boundary conditions (infinity)
 554   return commute(phase, this) ? this : NULL;
 555 }
 556 
 557 
 558 //=============================================================================
 559 //------------------------------Identity---------------------------------------
 560 // If one input is a constant 0, return the other input.
 561 Node* AddPNode::Identity(PhaseGVN* phase) {
 562   return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;
 563 }
 564 
 565 //------------------------------Idealize---------------------------------------
 566 Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 567   // Bail out if dead inputs
 568   if( phase->type( in(Address) ) == Type::TOP ) return NULL;
 569 
 570   // If the left input is an add of a constant, flatten the expression tree.
 571   const Node *n = in(Address);
 572   if (n->is_AddP() && n->in(Base) == in(Base)) {
 573     const AddPNode *addp = n->as_AddP(); // Left input is an AddP
 574     assert( !addp->in(Address)->is_AddP() ||
 575              addp->in(Address)->as_AddP() != addp,
 576             "dead loop in AddPNode::Ideal" );
 577     // Type of left input's right input
 578     const Type *t = phase->type( addp->in(Offset) );
 579     if( t == Type::TOP ) return NULL;
 580     const TypeX *t12 = t->is_intptr_t();
 581     if( t12->is_con() ) {       // Left input is an add of a constant?
 582       // If the right input is a constant, combine constants
 583       const Type *temp_t2 = phase->type( in(Offset) );
 584       if( temp_t2 == Type::TOP ) return NULL;
 585       const TypeX *t2 = temp_t2->is_intptr_t();
 586       Node* address;
 587       Node* offset;
 588       if( t2->is_con() ) {
 589         // The Add of the flattened expression
 590         address = addp->in(Address);
 591         offset  = phase->MakeConX(t2->get_con() + t12->get_con());
 592       } else {
 593         // Else move the constant to the right.  ((A+con)+B) into ((A+B)+con)
 594         address = phase->transform(new AddPNode(in(Base),addp->in(Address),in(Offset)));
 595         offset  = addp->in(Offset);
 596       }
 597       set_req_X(Address, address, phase);
 598       set_req_X(Offset, offset, phase);
 599       return this;
 600     }
 601   }
 602 
 603   // Raw pointers?
 604   if( in(Base)->bottom_type() == Type::TOP ) {
 605     // If this is a NULL+long form (from unsafe accesses), switch to a rawptr.
 606     if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
 607       Node* offset = in(Offset);
 608       return new CastX2PNode(offset);
 609     }
 610   }
 611 
 612   // If the right is an add of a constant, push the offset down.
 613   // Convert: (ptr + (offset+con)) into (ptr+offset)+con.
 614   // The idea is to merge array_base+scaled_index groups together,
 615   // and only have different constant offsets from the same base.
 616   const Node *add = in(Offset);
 617   if( add->Opcode() == Op_AddX && add->in(1) != add ) {
 618     const Type *t22 = phase->type( add->in(2) );
 619     if( t22->singleton() && (t22 != Type::TOP) ) {  // Right input is an add of a constant?
 620       set_req(Address, phase->transform(new AddPNode(in(Base),in(Address),add->in(1))));
 621       set_req_X(Offset, add->in(2), phase); // puts add on igvn worklist if needed
 622       return this;              // Made progress
 623     }
 624   }
 625 
 626   return NULL;                  // No progress
 627 }
 628 
 629 //------------------------------bottom_type------------------------------------
 630 // Bottom-type is the pointer-type with unknown offset.
 631 const Type *AddPNode::bottom_type() const {
 632   if (in(Address) == NULL)  return TypePtr::BOTTOM;
 633   const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
 634   if( !tp ) return Type::TOP;   // TOP input means TOP output
 635   assert( in(Offset)->Opcode() != Op_ConP, "" );
 636   const Type *t = in(Offset)->bottom_type();
 637   if( t == Type::TOP )
 638     return tp->add_offset(Type::OffsetTop);
 639   const TypeX *tx = t->is_intptr_t();
 640   intptr_t txoffset = Type::OffsetBot;
 641   if (tx->is_con()) {   // Left input is an add of a constant?
 642     txoffset = tx->get_con();
 643   }
 644   return tp->add_offset(txoffset);
 645 }
 646 
 647 //------------------------------Value------------------------------------------
 648 const Type* AddPNode::Value(PhaseGVN* phase) const {
 649   // Either input is TOP ==> the result is TOP
 650   const Type *t1 = phase->type( in(Address) );
 651   const Type *t2 = phase->type( in(Offset) );
 652   if( t1 == Type::TOP ) return Type::TOP;
 653   if( t2 == Type::TOP ) return Type::TOP;
 654 
 655   // Left input is a pointer
 656   const TypePtr *p1 = t1->isa_ptr();
 657   // Right input is an int
 658   const TypeX *p2 = t2->is_intptr_t();
 659   // Add 'em
 660   intptr_t p2offset = Type::OffsetBot;
 661   if (p2->is_con()) {   // Left input is an add of a constant?
 662     p2offset = p2->get_con();
 663   }
 664   return p1->add_offset(p2offset);
 665 }
 666 
 667 //------------------------Ideal_base_and_offset--------------------------------
 668 // Split an oop pointer into a base and offset.
 669 // (The offset might be Type::OffsetBot in the case of an array.)
 670 // Return the base, or NULL if failure.
 671 Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseTransform* phase,
 672                                       // second return value:
 673                                       intptr_t& offset) {
 674   if (ptr->is_AddP()) {
 675     Node* base = ptr->in(AddPNode::Base);
 676     Node* addr = ptr->in(AddPNode::Address);
 677     Node* offs = ptr->in(AddPNode::Offset);
 678     if (base == addr || base->is_top()) {
 679       offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
 680       if (offset != Type::OffsetBot) {
 681         return addr;
 682       }
 683     }
 684   }
 685   offset = Type::OffsetBot;
 686   return NULL;
 687 }
 688 
 689 //------------------------------unpack_offsets----------------------------------
 690 // Collect the AddP offset values into the elements array, giving up
 691 // if there are more than length.
 692 int AddPNode::unpack_offsets(Node* elements[], int length) {
 693   int count = 0;
 694   Node* addr = this;
 695   Node* base = addr->in(AddPNode::Base);
 696   while (addr->is_AddP()) {
 697     if (addr->in(AddPNode::Base) != base) {
 698       // give up
 699       return -1;
 700     }
 701     elements[count++] = addr->in(AddPNode::Offset);
 702     if (count == length) {
 703       // give up
 704       return -1;
 705     }
 706     addr = addr->in(AddPNode::Address);
 707   }
 708   if (addr != base) {
 709     return -1;
 710   }
 711   return count;
 712 }
 713 
 714 //------------------------------match_edge-------------------------------------
 715 // Do we Match on this edge index or not?  Do not match base pointer edge
 716 uint AddPNode::match_edge(uint idx) const {
 717   return idx > Base;
 718 }
 719 
 720 //=============================================================================
 721 //------------------------------Identity---------------------------------------
 722 Node* OrINode::Identity(PhaseGVN* phase) {
 723   // x | x => x
 724   if (in(1) == in(2)) {
 725     return in(1);
 726   }
 727 
 728   return AddNode::Identity(phase);
 729 }
 730 
 731 // Find shift value for Integer or Long OR.
 732 Node* rotate_shift(PhaseGVN* phase, Node* lshift, Node* rshift, int mask) {
 733   // val << norm_con_shift | val >> ({32|64} - norm_con_shift) => rotate_left val, norm_con_shift
 734   const TypeInt* lshift_t = phase->type(lshift)->isa_int();
 735   const TypeInt* rshift_t = phase->type(rshift)->isa_int();
 736   if (lshift_t != NULL && lshift_t->is_con() &&
 737       rshift_t != NULL && rshift_t->is_con() &&
 738       ((lshift_t->get_con() & mask) == ((mask + 1) - (rshift_t->get_con() & mask)))) {
 739     return phase->intcon(lshift_t->get_con() & mask);
 740   }
 741   // val << var_shift | val >> ({0|32|64} - var_shift) => rotate_left val, var_shift
 742   if (rshift->Opcode() == Op_SubI && rshift->in(2) == lshift && rshift->in(1)->is_Con()){
 743     const TypeInt* shift_t = phase->type(rshift->in(1))->isa_int();
 744     if (shift_t != NULL && shift_t->is_con() &&
 745         (shift_t->get_con() == 0 || shift_t->get_con() == (mask + 1))) {
 746       return lshift;
 747     }
 748   }
 749   return NULL;
 750 }
 751 
 752 Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 753   int lopcode = in(1)->Opcode();
 754   int ropcode = in(2)->Opcode();
 755   if (Matcher::match_rule_supported(Op_RotateLeft) &&
 756       lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) {
 757     Node* lshift = in(1)->in(2);
 758     Node* rshift = in(2)->in(2);
 759     Node* shift = rotate_shift(phase, lshift, rshift, 0x1F);
 760     if (shift != NULL) {
 761       return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT);
 762     }
 763     return NULL;
 764   }
 765   if (Matcher::match_rule_supported(Op_RotateRight) &&
 766       lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) {
 767     Node* rshift = in(1)->in(2);
 768     Node* lshift = in(2)->in(2);
 769     Node* shift = rotate_shift(phase, rshift, lshift, 0x1F);
 770     if (shift != NULL) {
 771       return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
 772     }
 773   }
 774   return NULL;
 775 }
 776 
 777 //------------------------------add_ring---------------------------------------
 778 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
 779 // the logical operations the ring's ADD is really a logical OR function.
 780 // This also type-checks the inputs for sanity.  Guaranteed never to
 781 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 782 const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
 783   const TypeInt *r0 = t0->is_int(); // Handy access
 784   const TypeInt *r1 = t1->is_int();
 785 
 786   // If both args are bool, can figure out better types
 787   if ( r0 == TypeInt::BOOL ) {
 788     if ( r1 == TypeInt::ONE) {
 789       return TypeInt::ONE;
 790     } else if ( r1 == TypeInt::BOOL ) {
 791       return TypeInt::BOOL;
 792     }
 793   } else if ( r0 == TypeInt::ONE ) {
 794     if ( r1 == TypeInt::BOOL ) {
 795       return TypeInt::ONE;
 796     }
 797   }
 798 
 799   // If either input is not a constant, just return all integers.
 800   if( !r0->is_con() || !r1->is_con() )
 801     return TypeInt::INT;        // Any integer, but still no symbols.
 802 
 803   // Otherwise just OR them bits.
 804   return TypeInt::make( r0->get_con() | r1->get_con() );
 805 }
 806 
 807 //=============================================================================
 808 //------------------------------Identity---------------------------------------
 809 Node* OrLNode::Identity(PhaseGVN* phase) {
 810   // x | x => x
 811   if (in(1) == in(2)) {
 812     return in(1);
 813   }
 814 
 815   return AddNode::Identity(phase);
 816 }
 817 
 818 Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 819   int lopcode = in(1)->Opcode();
 820   int ropcode = in(2)->Opcode();
 821   if (Matcher::match_rule_supported(Op_RotateLeft) &&
 822       lopcode == Op_LShiftL && ropcode == Op_URShiftL && in(1)->in(1) == in(2)->in(1)) {
 823     Node* lshift = in(1)->in(2);
 824     Node* rshift = in(2)->in(2);
 825     Node* shift = rotate_shift(phase, lshift, rshift, 0x3F);
 826     if (shift != NULL) {
 827       return new RotateLeftNode(in(1)->in(1), shift, TypeLong::LONG);
 828     }
 829     return NULL;
 830   }
 831   if (Matcher::match_rule_supported(Op_RotateRight) &&
 832       lopcode == Op_URShiftL && ropcode == Op_LShiftL && in(1)->in(1) == in(2)->in(1)) {
 833     Node* rshift = in(1)->in(2);
 834     Node* lshift = in(2)->in(2);
 835     Node* shift = rotate_shift(phase, rshift, lshift, 0x3F);
 836     if (shift != NULL) {
 837       return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG);
 838     }
 839   }
 840   return NULL;
 841 }
 842 
 843 //------------------------------add_ring---------------------------------------
 844 const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
 845   const TypeLong *r0 = t0->is_long(); // Handy access
 846   const TypeLong *r1 = t1->is_long();
 847 
 848   // If either input is not a constant, just return all integers.
 849   if( !r0->is_con() || !r1->is_con() )
 850     return TypeLong::LONG;      // Any integer, but still no symbols.
 851 
 852   // Otherwise just OR them bits.
 853   return TypeLong::make( r0->get_con() | r1->get_con() );
 854 }
 855 
 856 //---------------------------Helper -------------------------------------------
 857 /* Decide if the given node is used only in arithmetic expressions(add or sub).
 858  */
 859 static bool is_used_in_only_arithmetic(Node* n, BasicType bt) {
 860   for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
 861     Node* u = n->fast_out(i);
 862     if (u->Opcode() != Op_Add(bt) && u->Opcode() != Op_Sub(bt)) {
 863       return false;
 864     }
 865   }
 866   return true;
 867 }
 868 
 869 //=============================================================================
 870 //------------------------------Idealize---------------------------------------
 871 Node* XorINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 872   Node* in1 = in(1);
 873   Node* in2 = in(2);
 874 
 875   // Convert ~x into -1-x when ~x is used in an arithmetic expression
 876   // or x itself is an expression.
 877   if (phase->type(in2) == TypeInt::MINUS_1) { // follows LHS^(-1), i.e., ~LHS
 878     if (phase->is_IterGVN()) {
 879       if (is_used_in_only_arithmetic(this, T_INT)
 880           // LHS is arithmetic
 881           || (in1->Opcode() == Op_AddI || in1->Opcode() == Op_SubI)) {
 882         return new SubINode(in2, in1);
 883       }
 884     } else {
 885       // graph could be incomplete in GVN so we postpone to IGVN
 886       phase->record_for_igvn(this);
 887     }
 888   }
 889   return AddNode::Ideal(phase, can_reshape);
 890 }
 891 
 892 const Type* XorINode::Value(PhaseGVN* phase) const {
 893   Node* in1 = in(1);
 894   Node* in2 = in(2);
 895   const Type* t1 = phase->type(in1);
 896   const Type* t2 = phase->type(in2);
 897   if (t1 == Type::TOP || t2 == Type::TOP) {
 898     return Type::TOP;
 899   }
 900   // x ^ x ==> 0
 901   if (in1->eqv_uncast(in2)) {
 902     return add_id();
 903   }
 904   // result of xor can only have bits sets where any of the
 905   // inputs have bits set. lo can always become 0.
 906   const TypeInt* t1i = t1->is_int();
 907   const TypeInt* t2i = t2->is_int();
 908   if ((t1i->_lo >= 0) &&
 909       (t1i->_hi > 0)  &&
 910       (t2i->_lo >= 0) &&
 911       (t2i->_hi > 0)) {
 912     // hi - set all bits below the highest bit. Using round_down to avoid overflow.
 913     const TypeInt* t1x = TypeInt::make(0, round_down_power_of_2(t1i->_hi) + (round_down_power_of_2(t1i->_hi) - 1), t1i->_widen);
 914     const TypeInt* t2x = TypeInt::make(0, round_down_power_of_2(t2i->_hi) + (round_down_power_of_2(t2i->_hi) - 1), t2i->_widen);
 915     return t1x->meet(t2x);
 916   }
 917   return AddNode::Value(phase);
 918 }
 919 
 920 
 921 //------------------------------add_ring---------------------------------------
 922 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
 923 // the logical operations the ring's ADD is really a logical OR function.
 924 // This also type-checks the inputs for sanity.  Guaranteed never to
 925 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 926 const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
 927   const TypeInt *r0 = t0->is_int(); // Handy access
 928   const TypeInt *r1 = t1->is_int();
 929 
 930   // Complementing a boolean?
 931   if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE
 932                                || r1 == TypeInt::BOOL))
 933     return TypeInt::BOOL;
 934 
 935   if( !r0->is_con() || !r1->is_con() ) // Not constants
 936     return TypeInt::INT;        // Any integer, but still no symbols.
 937 
 938   // Otherwise just XOR them bits.
 939   return TypeInt::make( r0->get_con() ^ r1->get_con() );
 940 }
 941 
 942 //=============================================================================
 943 //------------------------------add_ring---------------------------------------
 944 const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
 945   const TypeLong *r0 = t0->is_long(); // Handy access
 946   const TypeLong *r1 = t1->is_long();
 947 
 948   // If either input is not a constant, just return all integers.
 949   if( !r0->is_con() || !r1->is_con() )
 950     return TypeLong::LONG;      // Any integer, but still no symbols.
 951 
 952   // Otherwise just OR them bits.
 953   return TypeLong::make( r0->get_con() ^ r1->get_con() );
 954 }
 955 
 956 Node* XorLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 957   Node* in1 = in(1);
 958   Node* in2 = in(2);
 959 
 960   // Convert ~x into -1-x when ~x is used in an arithmetic expression
 961   // or x itself is an arithmetic expression.
 962   if (phase->type(in2) == TypeLong::MINUS_1) { // follows LHS^(-1), i.e., ~LHS
 963     if (phase->is_IterGVN()) {
 964       if (is_used_in_only_arithmetic(this, T_LONG)
 965           // LHS is arithmetic
 966           || (in1->Opcode() == Op_AddL || in1->Opcode() == Op_SubL)) {
 967         return new SubLNode(in2, in1);
 968       }
 969     } else {
 970       // graph could be incomplete in GVN so we postpone to IGVN
 971       phase->record_for_igvn(this);
 972     }
 973   }
 974   return AddNode::Ideal(phase, can_reshape);
 975 }
 976 
 977 const Type* XorLNode::Value(PhaseGVN* phase) const {
 978   Node* in1 = in(1);
 979   Node* in2 = in(2);
 980   const Type* t1 = phase->type(in1);
 981   const Type* t2 = phase->type(in2);
 982   if (t1 == Type::TOP || t2 == Type::TOP) {
 983     return Type::TOP;
 984   }
 985   // x ^ x ==> 0
 986   if (in1->eqv_uncast(in2)) {
 987     return add_id();
 988   }
 989   // result of xor can only have bits sets where any of the
 990   // inputs have bits set. lo can always become 0.
 991   const TypeLong* t1l = t1->is_long();
 992   const TypeLong* t2l = t2->is_long();
 993   if ((t1l->_lo >= 0) &&
 994       (t1l->_hi > 0)  &&
 995       (t2l->_lo >= 0) &&
 996       (t2l->_hi > 0)) {
 997     // hi - set all bits below the highest bit. Using round_down to avoid overflow.
 998     const TypeLong* t1x = TypeLong::make(0, round_down_power_of_2(t1l->_hi) + (round_down_power_of_2(t1l->_hi) - 1), t1l->_widen);
 999     const TypeLong* t2x = TypeLong::make(0, round_down_power_of_2(t2l->_hi) + (round_down_power_of_2(t2l->_hi) - 1), t2l->_widen);
1000     return t1x->meet(t2x);
1001   }
1002   return AddNode::Value(phase);
1003 }
1004 
1005 Node* MaxNode::build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn) {
1006   bool is_int = gvn.type(a)->isa_int();
1007   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1008   assert(is_int == (gvn.type(b)->isa_int() != NULL), "inconsistent inputs");
1009   BasicType bt = is_int ? T_INT: T_LONG;
1010   Node* hook = NULL;
1011   if (gvn.is_IterGVN()) {
1012     // Make sure a and b are not destroyed
1013     hook = new Node(2);
1014     hook->init_req(0, a);
1015     hook->init_req(1, b);
1016   }
1017   Node* res = NULL;
1018   if (is_int && !is_unsigned) {
1019     if (is_max) {
1020       res =  gvn.transform(new MaxINode(a, b));
1021       assert(gvn.type(res)->is_int()->_lo >= t->is_int()->_lo && gvn.type(res)->is_int()->_hi <= t->is_int()->_hi, "type doesn't match");
1022     } else {
1023       Node* res =  gvn.transform(new MinINode(a, b));
1024       assert(gvn.type(res)->is_int()->_lo >= t->is_int()->_lo && gvn.type(res)->is_int()->_hi <= t->is_int()->_hi, "type doesn't match");
1025     }
1026   } else {
1027     Node* cmp = NULL;
1028     if (is_max) {
1029       cmp = gvn.transform(CmpNode::make(a, b, bt, is_unsigned));
1030     } else {
1031       cmp = gvn.transform(CmpNode::make(b, a, bt, is_unsigned));
1032     }
1033     Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1034     res = gvn.transform(CMoveNode::make(NULL, bol, a, b, t));
1035   }
1036   if (hook != NULL) {
1037     hook->destruct(&gvn);
1038   }
1039   return res;
1040 }
1041 
1042 Node* MaxNode::build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn) {
1043   bool is_int = gvn.type(a)->isa_int();
1044   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1045   assert(is_int == (gvn.type(b)->isa_int() != NULL), "inconsistent inputs");
1046   BasicType bt = is_int ? T_INT: T_LONG;
1047   Node* zero = gvn.integercon(0, bt);
1048   Node* hook = NULL;
1049   if (gvn.is_IterGVN()) {
1050     // Make sure a and b are not destroyed
1051     hook = new Node(2);
1052     hook->init_req(0, a);
1053     hook->init_req(1, b);
1054   }
1055   Node* cmp = NULL;
1056   if (is_max) {
1057     cmp = gvn.transform(CmpNode::make(a, b, bt, false));
1058   } else {
1059     cmp = gvn.transform(CmpNode::make(b, a, bt, false));
1060   }
1061   Node* sub = gvn.transform(SubNode::make(a, b, bt));
1062   Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1063   Node* res = gvn.transform(CMoveNode::make(NULL, bol, sub, zero, t));
1064   if (hook != NULL) {
1065     hook->destruct(&gvn);
1066   }
1067   return res;
1068 }
1069 
1070 //=============================================================================
1071 //------------------------------add_ring---------------------------------------
1072 // Supplied function returns the sum of the inputs.
1073 const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
1074   const TypeInt *r0 = t0->is_int(); // Handy access
1075   const TypeInt *r1 = t1->is_int();
1076 
1077   // Otherwise just MAX them bits.
1078   return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1079 }
1080 
1081 // Check if addition of an integer with type 't' and a constant 'c' can overflow
1082 static bool can_overflow(const TypeInt* t, jint c) {
1083   jint t_lo = t->_lo;
1084   jint t_hi = t->_hi;
1085   return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1086           (c > 0 && (java_add(t_hi, c) < t_hi)));
1087 }
1088 
1089 // Ideal transformations for MaxINode
1090 Node* MaxINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1091   // Force a right-spline graph
1092   Node* l = in(1);
1093   Node* r = in(2);
1094   // Transform  MaxI1(MaxI2(a, b), c)  into  MaxI1(a, MaxI2(b, c))
1095   // to force a right-spline graph for the rest of MaxINode::Ideal().
1096   if (l->Opcode() == Op_MaxI) {
1097     assert(l != l->in(1), "dead loop in MaxINode::Ideal");
1098     r = phase->transform(new MaxINode(l->in(2), r));
1099     l = l->in(1);
1100     set_req_X(1, l, phase);
1101     set_req_X(2, r, phase);
1102     return this;
1103   }
1104 
1105   // Get left input & constant
1106   Node* x = l;
1107   jint x_off = 0;
1108   if (x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant
1109       x->in(2)->is_Con()) {
1110     const Type* t = x->in(2)->bottom_type();
1111     if (t == Type::TOP) return NULL;  // No progress
1112     x_off = t->is_int()->get_con();
1113     x = x->in(1);
1114   }
1115 
1116   // Scan a right-spline-tree for MAXs
1117   Node* y = r;
1118   jint y_off = 0;
1119   // Check final part of MAX tree
1120   if (y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant
1121       y->in(2)->is_Con()) {
1122     const Type* t = y->in(2)->bottom_type();
1123     if (t == Type::TOP) return NULL;  // No progress
1124     y_off = t->is_int()->get_con();
1125     y = y->in(1);
1126   }
1127   if (x->_idx > y->_idx && r->Opcode() != Op_MaxI) {
1128     swap_edges(1, 2);
1129     return this;
1130   }
1131 
1132   const TypeInt* tx = phase->type(x)->isa_int();
1133 
1134   if (r->Opcode() == Op_MaxI) {
1135     assert(r != r->in(2), "dead loop in MaxINode::Ideal");
1136     y = r->in(1);
1137     // Check final part of MAX tree
1138     if (y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant
1139         y->in(2)->is_Con()) {
1140       const Type* t = y->in(2)->bottom_type();
1141       if (t == Type::TOP) return NULL;  // No progress
1142       y_off = t->is_int()->get_con();
1143       y = y->in(1);
1144     }
1145 
1146     if (x->_idx > y->_idx)
1147       return new MaxINode(r->in(1), phase->transform(new MaxINode(l, r->in(2))));
1148 
1149     // Transform MAX2(x + c0, MAX2(x + c1, z)) into MAX2(x + MAX2(c0, c1), z)
1150     // if x == y and the additions can't overflow.
1151     if (x == y && tx != NULL &&
1152         !can_overflow(tx, x_off) &&
1153         !can_overflow(tx, y_off)) {
1154       return new MaxINode(phase->transform(new AddINode(x, phase->intcon(MAX2(x_off, y_off)))), r->in(2));
1155     }
1156   } else {
1157     // Transform MAX2(x + c0, y + c1) into x + MAX2(c0, c1)
1158     // if x == y and the additions can't overflow.
1159     if (x == y && tx != NULL &&
1160         !can_overflow(tx, x_off) &&
1161         !can_overflow(tx, y_off)) {
1162       return new AddINode(x, phase->intcon(MAX2(x_off, y_off)));
1163     }
1164   }
1165  return NULL;
1166 }
1167 
1168 //=============================================================================
1169 //------------------------------Idealize---------------------------------------
1170 // MINs show up in range-check loop limit calculations.  Look for
1171 // "MIN2(x+c0,MIN2(y,x+c1))".  Pick the smaller constant: "MIN2(x+c0,y)"
1172 Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) {
1173   Node *progress = NULL;
1174   // Force a right-spline graph
1175   Node *l = in(1);
1176   Node *r = in(2);
1177   // Transform  MinI1( MinI2(a,b), c)  into  MinI1( a, MinI2(b,c) )
1178   // to force a right-spline graph for the rest of MinINode::Ideal().
1179   if( l->Opcode() == Op_MinI ) {
1180     assert( l != l->in(1), "dead loop in MinINode::Ideal" );
1181     r = phase->transform(new MinINode(l->in(2),r));
1182     l = l->in(1);
1183     set_req_X(1, l, phase);
1184     set_req_X(2, r, phase);
1185     return this;
1186   }
1187 
1188   // Get left input & constant
1189   Node *x = l;
1190   jint x_off = 0;
1191   if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant
1192       x->in(2)->is_Con() ) {
1193     const Type *t = x->in(2)->bottom_type();
1194     if( t == Type::TOP ) return NULL;  // No progress
1195     x_off = t->is_int()->get_con();
1196     x = x->in(1);
1197   }
1198 
1199   // Scan a right-spline-tree for MINs
1200   Node *y = r;
1201   jint y_off = 0;
1202   // Check final part of MIN tree
1203   if( y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant
1204       y->in(2)->is_Con() ) {
1205     const Type *t = y->in(2)->bottom_type();
1206     if( t == Type::TOP ) return NULL;  // No progress
1207     y_off = t->is_int()->get_con();
1208     y = y->in(1);
1209   }
1210   if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) {
1211     swap_edges(1, 2);
1212     return this;
1213   }
1214 
1215   const TypeInt* tx = phase->type(x)->isa_int();
1216 
1217   if( r->Opcode() == Op_MinI ) {
1218     assert( r != r->in(2), "dead loop in MinINode::Ideal" );
1219     y = r->in(1);
1220     // Check final part of MIN tree
1221     if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant
1222         y->in(2)->is_Con() ) {
1223       const Type *t = y->in(2)->bottom_type();
1224       if( t == Type::TOP ) return NULL;  // No progress
1225       y_off = t->is_int()->get_con();
1226       y = y->in(1);
1227     }
1228 
1229     if( x->_idx > y->_idx )
1230       return new MinINode(r->in(1),phase->transform(new MinINode(l,r->in(2))));
1231 
1232     // Transform MIN2(x + c0, MIN2(x + c1, z)) into MIN2(x + MIN2(c0, c1), z)
1233     // if x == y and the additions can't overflow.
1234     if (x == y && tx != NULL &&
1235         !can_overflow(tx, x_off) &&
1236         !can_overflow(tx, y_off)) {
1237       return new MinINode(phase->transform(new AddINode(x, phase->intcon(MIN2(x_off, y_off)))), r->in(2));
1238     }
1239   } else {
1240     // Transform MIN2(x + c0, y + c1) into x + MIN2(c0, c1)
1241     // if x == y and the additions can't overflow.
1242     if (x == y && tx != NULL &&
1243         !can_overflow(tx, x_off) &&
1244         !can_overflow(tx, y_off)) {
1245       return new AddINode(x,phase->intcon(MIN2(x_off,y_off)));
1246     }
1247   }
1248   return NULL;
1249 }
1250 
1251 //------------------------------add_ring---------------------------------------
1252 // Supplied function returns the sum of the inputs.
1253 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
1254   const TypeInt *r0 = t0->is_int(); // Handy access
1255   const TypeInt *r1 = t1->is_int();
1256 
1257   // Otherwise just MIN them bits.
1258   return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1259 }
1260 
1261 //------------------------------add_ring---------------------------------------
1262 const Type *MinFNode::add_ring( const Type *t0, const Type *t1 ) const {
1263   const TypeF *r0 = t0->is_float_constant();
1264   const TypeF *r1 = t1->is_float_constant();
1265 
1266   if (r0->is_nan()) {
1267     return r0;
1268   }
1269   if (r1->is_nan()) {
1270     return r1;
1271   }
1272 
1273   float f0 = r0->getf();
1274   float f1 = r1->getf();
1275   if (f0 != 0.0f || f1 != 0.0f) {
1276     return f0 < f1 ? r0 : r1;
1277   }
1278 
1279   // handle min of 0.0, -0.0 case.
1280   return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1;
1281 }
1282 
1283 //------------------------------add_ring---------------------------------------
1284 const Type *MinDNode::add_ring( const Type *t0, const Type *t1 ) const {
1285   const TypeD *r0 = t0->is_double_constant();
1286   const TypeD *r1 = t1->is_double_constant();
1287 
1288   if (r0->is_nan()) {
1289     return r0;
1290   }
1291   if (r1->is_nan()) {
1292     return r1;
1293   }
1294 
1295   double d0 = r0->getd();
1296   double d1 = r1->getd();
1297   if (d0 != 0.0 || d1 != 0.0) {
1298     return d0 < d1 ? r0 : r1;
1299   }
1300 
1301   // handle min of 0.0, -0.0 case.
1302   return (jlong_cast(d0) < jlong_cast(d1)) ? r0 : r1;
1303 }
1304 
1305 //------------------------------add_ring---------------------------------------
1306 const Type *MaxFNode::add_ring( const Type *t0, const Type *t1 ) const {
1307   const TypeF *r0 = t0->is_float_constant();
1308   const TypeF *r1 = t1->is_float_constant();
1309 
1310   if (r0->is_nan()) {
1311     return r0;
1312   }
1313   if (r1->is_nan()) {
1314     return r1;
1315   }
1316 
1317   float f0 = r0->getf();
1318   float f1 = r1->getf();
1319   if (f0 != 0.0f || f1 != 0.0f) {
1320     return f0 > f1 ? r0 : r1;
1321   }
1322 
1323   // handle max of 0.0,-0.0 case.
1324   return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1;
1325 }
1326 
1327 //------------------------------add_ring---------------------------------------
1328 const Type *MaxDNode::add_ring( const Type *t0, const Type *t1 ) const {
1329   const TypeD *r0 = t0->is_double_constant();
1330   const TypeD *r1 = t1->is_double_constant();
1331 
1332   if (r0->is_nan()) {
1333     return r0;
1334   }
1335   if (r1->is_nan()) {
1336     return r1;
1337   }
1338 
1339   double d0 = r0->getd();
1340   double d1 = r1->getd();
1341   if (d0 != 0.0 || d1 != 0.0) {
1342     return d0 > d1 ? r0 : r1;
1343   }
1344 
1345   // handle max of 0.0, -0.0 case.
1346   return (jlong_cast(d0) > jlong_cast(d1)) ? r0 : r1;
1347 }