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   // Convert (~x+c) into (c-1)-x. Note there isn't a bitwise not
 368   // bytecode, "~x" would typically represented as "x^(-1)", so (~x+c)
 369   // will be (x^(-1))+c.
 370   if (op1 == Op_Xor(bt) &&
 371       (in2->Opcode() == Op_ConI || in2->Opcode() == Op_ConL) &&
 372       phase->type(in1->in(2)) == TypeInteger::minus_1(bt)) {
 373     Node* c_minus_one = phase->makecon(add_ring(phase->type(in(2)), TypeInteger::minus_1(bt)));
 374     return SubNode::make(c_minus_one, in1->in(1), bt);
 375   }
 376   return AddNode::Ideal(phase, can_reshape);
 377 }
 378 
 379 
 380 Node* AddINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 381   Node* in1 = in(1);
 382   Node* in2 = in(2);
 383   int op1 = in1->Opcode();
 384   int op2 = in2->Opcode();
 385 
 386   // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
 387   // Helps with array allocation math constant folding
 388   // See 4790063:
 389   // Unrestricted transformation is unsafe for some runtime values of 'x'
 390   // ( x ==  0, z == 1, y == -1 ) fails
 391   // ( x == -5, z == 1, y ==  1 ) fails
 392   // Transform works for small z and small negative y when the addition
 393   // (x + (y << z)) does not cross zero.
 394   // Implement support for negative y and (x >= -(y << z))
 395   // Have not observed cases where type information exists to support
 396   // positive y and (x <= -(y << z))
 397   if (op1 == Op_URShiftI && op2 == Op_ConI &&
 398       in1->in(2)->Opcode() == Op_ConI) {
 399     jint z = phase->type(in1->in(2))->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
 400     jint y = phase->type(in2)->is_int()->get_con();
 401 
 402     if (z < 5 && -5 < y && y < 0) {
 403       const Type* t_in11 = phase->type(in1->in(1));
 404       if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z))) {
 405         Node* a = phase->transform(new AddINode( in1->in(1), phase->intcon(y<<z)));
 406         return new URShiftINode(a, in1->in(2));
 407       }
 408     }
 409   }
 410 
 411   return AddNode::IdealIL(phase, can_reshape, T_INT);
 412 }
 413 
 414 
 415 //------------------------------Identity---------------------------------------
 416 // Fold (x-y)+y  OR  y+(x-y)  into  x
 417 Node* AddINode::Identity(PhaseGVN* phase) {
 418   if (in(1)->Opcode() == Op_SubI && in(1)->in(2) == in(2)) {
 419     return in(1)->in(1);
 420   } else if (in(2)->Opcode() == Op_SubI && in(2)->in(2) == in(1)) {
 421     return in(2)->in(1);
 422   }
 423   return AddNode::Identity(phase);
 424 }
 425 
 426 
 427 //------------------------------add_ring---------------------------------------
 428 // Supplied function returns the sum of the inputs.  Guaranteed never
 429 // to be passed a TOP or BOTTOM type, these are filtered out by
 430 // pre-check.
 431 const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {
 432   const TypeInt *r0 = t0->is_int(); // Handy access
 433   const TypeInt *r1 = t1->is_int();
 434   int lo = java_add(r0->_lo, r1->_lo);
 435   int hi = java_add(r0->_hi, r1->_hi);
 436   if( !(r0->is_con() && r1->is_con()) ) {
 437     // Not both constants, compute approximate result
 438     if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
 439       lo = min_jint; hi = max_jint; // Underflow on the low side
 440     }
 441     if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
 442       lo = min_jint; hi = max_jint; // Overflow on the high side
 443     }
 444     if( lo > hi ) {               // Handle overflow
 445       lo = min_jint; hi = max_jint;
 446     }
 447   } else {
 448     // both constants, compute precise result using 'lo' and 'hi'
 449     // Semantics define overflow and underflow for integer addition
 450     // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
 451   }
 452   return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
 453 }
 454 
 455 
 456 //=============================================================================
 457 //------------------------------Idealize---------------------------------------
 458 Node* AddLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 459   return AddNode::IdealIL(phase, can_reshape, T_LONG);
 460 }
 461 
 462 
 463 //------------------------------Identity---------------------------------------
 464 // Fold (x-y)+y  OR  y+(x-y)  into  x
 465 Node* AddLNode::Identity(PhaseGVN* phase) {
 466   if (in(1)->Opcode() == Op_SubL && in(1)->in(2) == in(2)) {
 467     return in(1)->in(1);
 468   } else if (in(2)->Opcode() == Op_SubL && in(2)->in(2) == in(1)) {
 469     return in(2)->in(1);
 470   }
 471   return AddNode::Identity(phase);
 472 }
 473 
 474 
 475 //------------------------------add_ring---------------------------------------
 476 // Supplied function returns the sum of the inputs.  Guaranteed never
 477 // to be passed a TOP or BOTTOM type, these are filtered out by
 478 // pre-check.
 479 const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {
 480   const TypeLong *r0 = t0->is_long(); // Handy access
 481   const TypeLong *r1 = t1->is_long();
 482   jlong lo = java_add(r0->_lo, r1->_lo);
 483   jlong hi = java_add(r0->_hi, r1->_hi);
 484   if( !(r0->is_con() && r1->is_con()) ) {
 485     // Not both constants, compute approximate result
 486     if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
 487       lo =min_jlong; hi = max_jlong; // Underflow on the low side
 488     }
 489     if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
 490       lo = min_jlong; hi = max_jlong; // Overflow on the high side
 491     }
 492     if( lo > hi ) {               // Handle overflow
 493       lo = min_jlong; hi = max_jlong;
 494     }
 495   } else {
 496     // both constants, compute precise result using 'lo' and 'hi'
 497     // Semantics define overflow and underflow for integer addition
 498     // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
 499   }
 500   return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
 501 }
 502 
 503 
 504 //=============================================================================
 505 //------------------------------add_of_identity--------------------------------
 506 // Check for addition of the identity
 507 const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {
 508   // x ADD 0  should return x unless 'x' is a -zero
 509   //
 510   // const Type *zero = add_id();     // The additive identity
 511   // jfloat f1 = t1->getf();
 512   // jfloat f2 = t2->getf();
 513   //
 514   // if( t1->higher_equal( zero ) ) return t2;
 515   // if( t2->higher_equal( zero ) ) return t1;
 516 
 517   return NULL;
 518 }
 519 
 520 //------------------------------add_ring---------------------------------------
 521 // Supplied function returns the sum of the inputs.
 522 // This also type-checks the inputs for sanity.  Guaranteed never to
 523 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 524 const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {
 525   // We must be adding 2 float constants.
 526   return TypeF::make( t0->getf() + t1->getf() );
 527 }
 528 
 529 //------------------------------Ideal------------------------------------------
 530 Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 531   // Floating point additions are not associative because of boundary conditions (infinity)
 532   return commute(phase, this) ? this : NULL;
 533 }
 534 
 535 
 536 //=============================================================================
 537 //------------------------------add_of_identity--------------------------------
 538 // Check for addition of the identity
 539 const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {
 540   // x ADD 0  should return x unless 'x' is a -zero
 541   //
 542   // const Type *zero = add_id();     // The additive identity
 543   // jfloat f1 = t1->getf();
 544   // jfloat f2 = t2->getf();
 545   //
 546   // if( t1->higher_equal( zero ) ) return t2;
 547   // if( t2->higher_equal( zero ) ) return t1;
 548 
 549   return NULL;
 550 }
 551 //------------------------------add_ring---------------------------------------
 552 // Supplied function returns the sum of the inputs.
 553 // This also type-checks the inputs for sanity.  Guaranteed never to
 554 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 555 const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {
 556   // We must be adding 2 double constants.
 557   return TypeD::make( t0->getd() + t1->getd() );
 558 }
 559 
 560 //------------------------------Ideal------------------------------------------
 561 Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 562   // Floating point additions are not associative because of boundary conditions (infinity)
 563   return commute(phase, this) ? this : NULL;
 564 }
 565 
 566 
 567 //=============================================================================
 568 //------------------------------Identity---------------------------------------
 569 // If one input is a constant 0, return the other input.
 570 Node* AddPNode::Identity(PhaseGVN* phase) {
 571   return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;
 572 }
 573 
 574 //------------------------------Idealize---------------------------------------
 575 Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 576   // Bail out if dead inputs
 577   if( phase->type( in(Address) ) == Type::TOP ) return NULL;
 578 
 579   // If the left input is an add of a constant, flatten the expression tree.
 580   const Node *n = in(Address);
 581   if (n->is_AddP() && n->in(Base) == in(Base)) {
 582     const AddPNode *addp = n->as_AddP(); // Left input is an AddP
 583     assert( !addp->in(Address)->is_AddP() ||
 584              addp->in(Address)->as_AddP() != addp,
 585             "dead loop in AddPNode::Ideal" );
 586     // Type of left input's right input
 587     const Type *t = phase->type( addp->in(Offset) );
 588     if( t == Type::TOP ) return NULL;
 589     const TypeX *t12 = t->is_intptr_t();
 590     if( t12->is_con() ) {       // Left input is an add of a constant?
 591       // If the right input is a constant, combine constants
 592       const Type *temp_t2 = phase->type( in(Offset) );
 593       if( temp_t2 == Type::TOP ) return NULL;
 594       const TypeX *t2 = temp_t2->is_intptr_t();
 595       Node* address;
 596       Node* offset;
 597       if( t2->is_con() ) {
 598         // The Add of the flattened expression
 599         address = addp->in(Address);
 600         offset  = phase->MakeConX(t2->get_con() + t12->get_con());
 601       } else {
 602         // Else move the constant to the right.  ((A+con)+B) into ((A+B)+con)
 603         address = phase->transform(new AddPNode(in(Base),addp->in(Address),in(Offset)));
 604         offset  = addp->in(Offset);
 605       }
 606       set_req_X(Address, address, phase);
 607       set_req_X(Offset, offset, phase);
 608       return this;
 609     }
 610   }
 611 
 612   // Raw pointers?
 613   if( in(Base)->bottom_type() == Type::TOP ) {
 614     // If this is a NULL+long form (from unsafe accesses), switch to a rawptr.
 615     if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
 616       Node* offset = in(Offset);
 617       return new CastX2PNode(offset);
 618     }
 619   }
 620 
 621   // If the right is an add of a constant, push the offset down.
 622   // Convert: (ptr + (offset+con)) into (ptr+offset)+con.
 623   // The idea is to merge array_base+scaled_index groups together,
 624   // and only have different constant offsets from the same base.
 625   const Node *add = in(Offset);
 626   if( add->Opcode() == Op_AddX && add->in(1) != add ) {
 627     const Type *t22 = phase->type( add->in(2) );
 628     if( t22->singleton() && (t22 != Type::TOP) ) {  // Right input is an add of a constant?
 629       set_req(Address, phase->transform(new AddPNode(in(Base),in(Address),add->in(1))));
 630       set_req_X(Offset, add->in(2), phase); // puts add on igvn worklist if needed
 631       return this;              // Made progress
 632     }
 633   }
 634 
 635   return NULL;                  // No progress
 636 }
 637 
 638 //------------------------------bottom_type------------------------------------
 639 // Bottom-type is the pointer-type with unknown offset.
 640 const Type *AddPNode::bottom_type() const {
 641   if (in(Address) == NULL)  return TypePtr::BOTTOM;
 642   const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
 643   if( !tp ) return Type::TOP;   // TOP input means TOP output
 644   assert( in(Offset)->Opcode() != Op_ConP, "" );
 645   const Type *t = in(Offset)->bottom_type();
 646   if( t == Type::TOP )
 647     return tp->add_offset(Type::OffsetTop);
 648   const TypeX *tx = t->is_intptr_t();
 649   intptr_t txoffset = Type::OffsetBot;
 650   if (tx->is_con()) {   // Left input is an add of a constant?
 651     txoffset = tx->get_con();
 652   }
 653   if (tp->isa_aryptr()) {
 654     // In the case of a flattened inline type array, each field has its
 655     // own slice so we need to extract the field being accessed from
 656     // the address computation
 657     return tp->is_aryptr()->add_field_offset_and_offset(txoffset);
 658   }
 659   return tp->add_offset(txoffset);
 660 }
 661 
 662 //------------------------------Value------------------------------------------
 663 const Type* AddPNode::Value(PhaseGVN* phase) const {
 664   // Either input is TOP ==> the result is TOP
 665   const Type *t1 = phase->type( in(Address) );
 666   const Type *t2 = phase->type( in(Offset) );
 667   if( t1 == Type::TOP ) return Type::TOP;
 668   if( t2 == Type::TOP ) return Type::TOP;
 669 
 670   // Left input is a pointer
 671   const TypePtr *p1 = t1->isa_ptr();
 672   // Right input is an int
 673   const TypeX *p2 = t2->is_intptr_t();
 674   // Add 'em
 675   intptr_t p2offset = Type::OffsetBot;
 676   if (p2->is_con()) {   // Left input is an add of a constant?
 677     p2offset = p2->get_con();
 678   }
 679   if (p1->isa_aryptr()) {
 680     // In the case of a flattened inline type array, each field has its
 681     // own slice so we need to extract the field being accessed from
 682     // the address computation
 683     return p1->is_aryptr()->add_field_offset_and_offset(p2offset);
 684   }
 685   return p1->add_offset(p2offset);
 686 }
 687 
 688 //------------------------Ideal_base_and_offset--------------------------------
 689 // Split an oop pointer into a base and offset.
 690 // (The offset might be Type::OffsetBot in the case of an array.)
 691 // Return the base, or NULL if failure.
 692 Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseTransform* phase,
 693                                       // second return value:
 694                                       intptr_t& offset) {
 695   if (ptr->is_AddP()) {
 696     Node* base = ptr->in(AddPNode::Base);
 697     Node* addr = ptr->in(AddPNode::Address);
 698     Node* offs = ptr->in(AddPNode::Offset);
 699     if (base == addr || base->is_top()) {
 700       offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
 701       if (offset != Type::OffsetBot) {
 702         return addr;
 703       }
 704     }
 705   }
 706   offset = Type::OffsetBot;
 707   return NULL;
 708 }
 709 
 710 //------------------------------unpack_offsets----------------------------------
 711 // Collect the AddP offset values into the elements array, giving up
 712 // if there are more than length.
 713 int AddPNode::unpack_offsets(Node* elements[], int length) {
 714   int count = 0;
 715   Node* addr = this;
 716   Node* base = addr->in(AddPNode::Base);
 717   while (addr->is_AddP()) {
 718     if (addr->in(AddPNode::Base) != base) {
 719       // give up
 720       return -1;
 721     }
 722     elements[count++] = addr->in(AddPNode::Offset);
 723     if (count == length) {
 724       // give up
 725       return -1;
 726     }
 727     addr = addr->in(AddPNode::Address);
 728   }
 729   if (addr != base) {
 730     return -1;
 731   }
 732   return count;
 733 }
 734 
 735 //------------------------------match_edge-------------------------------------
 736 // Do we Match on this edge index or not?  Do not match base pointer edge
 737 uint AddPNode::match_edge(uint idx) const {
 738   return idx > Base;
 739 }
 740 
 741 //=============================================================================
 742 //------------------------------Identity---------------------------------------
 743 Node* OrINode::Identity(PhaseGVN* phase) {
 744   // x | x => x
 745   if (in(1) == in(2)) {
 746     return in(1);
 747   }
 748 
 749   return AddNode::Identity(phase);
 750 }
 751 
 752 // Find shift value for Integer or Long OR.
 753 Node* rotate_shift(PhaseGVN* phase, Node* lshift, Node* rshift, int mask) {
 754   // val << norm_con_shift | val >> ({32|64} - norm_con_shift) => rotate_left val, norm_con_shift
 755   const TypeInt* lshift_t = phase->type(lshift)->isa_int();
 756   const TypeInt* rshift_t = phase->type(rshift)->isa_int();
 757   if (lshift_t != NULL && lshift_t->is_con() &&
 758       rshift_t != NULL && rshift_t->is_con() &&
 759       ((lshift_t->get_con() & mask) == ((mask + 1) - (rshift_t->get_con() & mask)))) {
 760     return phase->intcon(lshift_t->get_con() & mask);
 761   }
 762   // val << var_shift | val >> ({0|32|64} - var_shift) => rotate_left val, var_shift
 763   if (rshift->Opcode() == Op_SubI && rshift->in(2) == lshift && rshift->in(1)->is_Con()){
 764     const TypeInt* shift_t = phase->type(rshift->in(1))->isa_int();
 765     if (shift_t != NULL && shift_t->is_con() &&
 766         (shift_t->get_con() == 0 || shift_t->get_con() == (mask + 1))) {
 767       return lshift;
 768     }
 769   }
 770   return NULL;
 771 }
 772 
 773 Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 774   int lopcode = in(1)->Opcode();
 775   int ropcode = in(2)->Opcode();
 776   if (Matcher::match_rule_supported(Op_RotateLeft) &&
 777       lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) {
 778     Node* lshift = in(1)->in(2);
 779     Node* rshift = in(2)->in(2);
 780     Node* shift = rotate_shift(phase, lshift, rshift, 0x1F);
 781     if (shift != NULL) {
 782       return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT);
 783     }
 784     return NULL;
 785   }
 786   if (Matcher::match_rule_supported(Op_RotateRight) &&
 787       lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) {
 788     Node* rshift = in(1)->in(2);
 789     Node* lshift = in(2)->in(2);
 790     Node* shift = rotate_shift(phase, rshift, lshift, 0x1F);
 791     if (shift != NULL) {
 792       return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
 793     }
 794   }
 795   return NULL;
 796 }
 797 
 798 //------------------------------add_ring---------------------------------------
 799 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
 800 // the logical operations the ring's ADD is really a logical OR function.
 801 // This also type-checks the inputs for sanity.  Guaranteed never to
 802 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 803 const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
 804   const TypeInt *r0 = t0->is_int(); // Handy access
 805   const TypeInt *r1 = t1->is_int();
 806 
 807   // If both args are bool, can figure out better types
 808   if ( r0 == TypeInt::BOOL ) {
 809     if ( r1 == TypeInt::ONE) {
 810       return TypeInt::ONE;
 811     } else if ( r1 == TypeInt::BOOL ) {
 812       return TypeInt::BOOL;
 813     }
 814   } else if ( r0 == TypeInt::ONE ) {
 815     if ( r1 == TypeInt::BOOL ) {
 816       return TypeInt::ONE;
 817     }
 818   }
 819 
 820   // If either input is not a constant, just return all integers.
 821   if( !r0->is_con() || !r1->is_con() )
 822     return TypeInt::INT;        // Any integer, but still no symbols.
 823 
 824   // Otherwise just OR them bits.
 825   return TypeInt::make( r0->get_con() | r1->get_con() );
 826 }
 827 
 828 //=============================================================================
 829 //------------------------------Identity---------------------------------------
 830 Node* OrLNode::Identity(PhaseGVN* phase) {
 831   // x | x => x
 832   if (in(1) == in(2)) {
 833     return in(1);
 834   }
 835 
 836   return AddNode::Identity(phase);
 837 }
 838 
 839 Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 840   int lopcode = in(1)->Opcode();
 841   int ropcode = in(2)->Opcode();
 842   if (Matcher::match_rule_supported(Op_RotateLeft) &&
 843       lopcode == Op_LShiftL && ropcode == Op_URShiftL && in(1)->in(1) == in(2)->in(1)) {
 844     Node* lshift = in(1)->in(2);
 845     Node* rshift = in(2)->in(2);
 846     Node* shift = rotate_shift(phase, lshift, rshift, 0x3F);
 847     if (shift != NULL) {
 848       return new RotateLeftNode(in(1)->in(1), shift, TypeLong::LONG);
 849     }
 850     return NULL;
 851   }
 852   if (Matcher::match_rule_supported(Op_RotateRight) &&
 853       lopcode == Op_URShiftL && ropcode == Op_LShiftL && in(1)->in(1) == in(2)->in(1)) {
 854     Node* rshift = in(1)->in(2);
 855     Node* lshift = in(2)->in(2);
 856     Node* shift = rotate_shift(phase, rshift, lshift, 0x3F);
 857     if (shift != NULL) {
 858       return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG);
 859     }
 860   }
 861   return NULL;
 862 }
 863 
 864 //------------------------------add_ring---------------------------------------
 865 const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
 866   const TypeLong *r0 = t0->is_long(); // Handy access
 867   const TypeLong *r1 = t1->is_long();
 868 
 869   // If either input is not a constant, just return all integers.
 870   if( !r0->is_con() || !r1->is_con() )
 871     return TypeLong::LONG;      // Any integer, but still no symbols.
 872 
 873   // Otherwise just OR them bits.
 874   return TypeLong::make( r0->get_con() | r1->get_con() );
 875 }
 876 
 877 //=============================================================================
 878 //------------------------------Idealize---------------------------------------
 879 Node* XorINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 880   Node* in1 = in(1);
 881   Node* in2 = in(2);
 882   int op1 = in1->Opcode();
 883   // Convert ~(x+c) into (-c-1)-x. Note there isn't a bitwise not
 884   // bytecode, "~x" would typically represented as "x^(-1)", so ~(x+c)
 885   // will eventually be (x+c)^-1.
 886   if (op1 == Op_AddI && phase->type(in2) == TypeInt::MINUS_1 &&
 887       in1->in(2)->Opcode() == Op_ConI) {
 888     jint c = phase->type(in1->in(2))->isa_int()->get_con();
 889     Node* neg_c_minus_one = phase->intcon(java_add(-c, -1));
 890     return new SubINode(neg_c_minus_one, in1->in(1));
 891   }
 892   return AddNode::Ideal(phase, can_reshape);
 893 }
 894 
 895 const Type* XorINode::Value(PhaseGVN* phase) const {
 896   Node* in1 = in(1);
 897   Node* in2 = in(2);
 898   const Type* t1 = phase->type(in1);
 899   const Type* t2 = phase->type(in2);
 900   if (t1 == Type::TOP || t2 == Type::TOP) {
 901     return Type::TOP;
 902   }
 903   // x ^ x ==> 0
 904   if (in1->eqv_uncast(in2)) {
 905     return add_id();
 906   }
 907   // result of xor can only have bits sets where any of the
 908   // inputs have bits set. lo can always become 0.
 909   const TypeInt* t1i = t1->is_int();
 910   const TypeInt* t2i = t2->is_int();
 911   if ((t1i->_lo >= 0) &&
 912       (t1i->_hi > 0)  &&
 913       (t2i->_lo >= 0) &&
 914       (t2i->_hi > 0)) {
 915     // hi - set all bits below the highest bit. Using round_down to avoid overflow.
 916     const TypeInt* t1x = TypeInt::make(0, round_down_power_of_2(t1i->_hi) + (round_down_power_of_2(t1i->_hi) - 1), t1i->_widen);
 917     const TypeInt* t2x = TypeInt::make(0, round_down_power_of_2(t2i->_hi) + (round_down_power_of_2(t2i->_hi) - 1), t2i->_widen);
 918     return t1x->meet(t2x);
 919   }
 920   return AddNode::Value(phase);
 921 }
 922 
 923 
 924 //------------------------------add_ring---------------------------------------
 925 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
 926 // the logical operations the ring's ADD is really a logical OR function.
 927 // This also type-checks the inputs for sanity.  Guaranteed never to
 928 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 929 const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
 930   const TypeInt *r0 = t0->is_int(); // Handy access
 931   const TypeInt *r1 = t1->is_int();
 932 
 933   // Complementing a boolean?
 934   if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE
 935                                || r1 == TypeInt::BOOL))
 936     return TypeInt::BOOL;
 937 
 938   if( !r0->is_con() || !r1->is_con() ) // Not constants
 939     return TypeInt::INT;        // Any integer, but still no symbols.
 940 
 941   // Otherwise just XOR them bits.
 942   return TypeInt::make( r0->get_con() ^ r1->get_con() );
 943 }
 944 
 945 //=============================================================================
 946 //------------------------------add_ring---------------------------------------
 947 const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
 948   const TypeLong *r0 = t0->is_long(); // Handy access
 949   const TypeLong *r1 = t1->is_long();
 950 
 951   // If either input is not a constant, just return all integers.
 952   if( !r0->is_con() || !r1->is_con() )
 953     return TypeLong::LONG;      // Any integer, but still no symbols.
 954 
 955   // Otherwise just OR them bits.
 956   return TypeLong::make( r0->get_con() ^ r1->get_con() );
 957 }
 958 
 959 Node* XorLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 960   Node* in1 = in(1);
 961   Node* in2 = in(2);
 962   int op1 = in1->Opcode();
 963   // Convert ~(x+c) into (-c-1)-x. Note there isn't a bitwise not
 964   // bytecode, "~x" would typically represented as "x^(-1)", so ~(x+c)
 965   // will eventually be (x+c)^-1.
 966   if (op1 == Op_AddL && phase->type(in2) == TypeLong::MINUS_1 &&
 967       in1->in(2)->Opcode() == Op_ConL) {
 968     jlong c = phase->type(in1->in(2))->isa_long()->get_con();
 969     Node* neg_c_minus_one = phase->longcon(java_add(-c, (jlong)-1));
 970     return new SubLNode(neg_c_minus_one, in1->in(1));
 971   }
 972   return AddNode::Ideal(phase, can_reshape);
 973 }
 974 
 975 const Type* XorLNode::Value(PhaseGVN* phase) const {
 976   Node* in1 = in(1);
 977   Node* in2 = in(2);
 978   const Type* t1 = phase->type(in1);
 979   const Type* t2 = phase->type(in2);
 980   if (t1 == Type::TOP || t2 == Type::TOP) {
 981     return Type::TOP;
 982   }
 983   // x ^ x ==> 0
 984   if (in1->eqv_uncast(in2)) {
 985     return add_id();
 986   }
 987   // result of xor can only have bits sets where any of the
 988   // inputs have bits set. lo can always become 0.
 989   const TypeLong* t1l = t1->is_long();
 990   const TypeLong* t2l = t2->is_long();
 991   if ((t1l->_lo >= 0) &&
 992       (t1l->_hi > 0)  &&
 993       (t2l->_lo >= 0) &&
 994       (t2l->_hi > 0)) {
 995     // hi - set all bits below the highest bit. Using round_down to avoid overflow.
 996     const TypeLong* t1x = TypeLong::make(0, round_down_power_of_2(t1l->_hi) + (round_down_power_of_2(t1l->_hi) - 1), t1l->_widen);
 997     const TypeLong* t2x = TypeLong::make(0, round_down_power_of_2(t2l->_hi) + (round_down_power_of_2(t2l->_hi) - 1), t2l->_widen);
 998     return t1x->meet(t2x);
 999   }
1000   return AddNode::Value(phase);
1001 }
1002 
1003 Node* MaxNode::build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn) {
1004   bool is_int = gvn.type(a)->isa_int();
1005   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1006   assert(is_int == (gvn.type(b)->isa_int() != NULL), "inconsistent inputs");
1007   BasicType bt = is_int ? T_INT: T_LONG;
1008   Node* hook = NULL;
1009   if (gvn.is_IterGVN()) {
1010     // Make sure a and b are not destroyed
1011     hook = new Node(2);
1012     hook->init_req(0, a);
1013     hook->init_req(1, b);
1014   }
1015   Node* res = NULL;
1016   if (is_int && !is_unsigned) {
1017     if (is_max) {
1018       res =  gvn.transform(new MaxINode(a, b));
1019       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");
1020     } else {
1021       Node* res =  gvn.transform(new MinINode(a, b));
1022       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");
1023     }
1024   } else {
1025     Node* cmp = NULL;
1026     if (is_max) {
1027       cmp = gvn.transform(CmpNode::make(a, b, bt, is_unsigned));
1028     } else {
1029       cmp = gvn.transform(CmpNode::make(b, a, bt, is_unsigned));
1030     }
1031     Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1032     res = gvn.transform(CMoveNode::make(NULL, bol, a, b, t));
1033   }
1034   if (hook != NULL) {
1035     hook->destruct(&gvn);
1036   }
1037   return res;
1038 }
1039 
1040 Node* MaxNode::build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn) {
1041   bool is_int = gvn.type(a)->isa_int();
1042   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1043   assert(is_int == (gvn.type(b)->isa_int() != NULL), "inconsistent inputs");
1044   BasicType bt = is_int ? T_INT: T_LONG;
1045   Node* zero = gvn.integercon(0, bt);
1046   Node* hook = NULL;
1047   if (gvn.is_IterGVN()) {
1048     // Make sure a and b are not destroyed
1049     hook = new Node(2);
1050     hook->init_req(0, a);
1051     hook->init_req(1, b);
1052   }
1053   Node* cmp = NULL;
1054   if (is_max) {
1055     cmp = gvn.transform(CmpNode::make(a, b, bt, false));
1056   } else {
1057     cmp = gvn.transform(CmpNode::make(b, a, bt, false));
1058   }
1059   Node* sub = gvn.transform(SubNode::make(a, b, bt));
1060   Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1061   Node* res = gvn.transform(CMoveNode::make(NULL, bol, sub, zero, t));
1062   if (hook != NULL) {
1063     hook->destruct(&gvn);
1064   }
1065   return res;
1066 }
1067 
1068 //=============================================================================
1069 //------------------------------add_ring---------------------------------------
1070 // Supplied function returns the sum of the inputs.
1071 const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
1072   const TypeInt *r0 = t0->is_int(); // Handy access
1073   const TypeInt *r1 = t1->is_int();
1074 
1075   // Otherwise just MAX them bits.
1076   return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1077 }
1078 
1079 // Check if addition of an integer with type 't' and a constant 'c' can overflow
1080 static bool can_overflow(const TypeInt* t, jint c) {
1081   jint t_lo = t->_lo;
1082   jint t_hi = t->_hi;
1083   return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1084           (c > 0 && (java_add(t_hi, c) < t_hi)));
1085 }
1086 
1087 // Ideal transformations for MaxINode
1088 Node* MaxINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1089   // Force a right-spline graph
1090   Node* l = in(1);
1091   Node* r = in(2);
1092   // Transform  MaxI1(MaxI2(a, b), c)  into  MaxI1(a, MaxI2(b, c))
1093   // to force a right-spline graph for the rest of MaxINode::Ideal().
1094   if (l->Opcode() == Op_MaxI) {
1095     assert(l != l->in(1), "dead loop in MaxINode::Ideal");
1096     r = phase->transform(new MaxINode(l->in(2), r));
1097     l = l->in(1);
1098     set_req_X(1, l, phase);
1099     set_req_X(2, r, phase);
1100     return this;
1101   }
1102 
1103   // Get left input & constant
1104   Node* x = l;
1105   jint x_off = 0;
1106   if (x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant
1107       x->in(2)->is_Con()) {
1108     const Type* t = x->in(2)->bottom_type();
1109     if (t == Type::TOP) return NULL;  // No progress
1110     x_off = t->is_int()->get_con();
1111     x = x->in(1);
1112   }
1113 
1114   // Scan a right-spline-tree for MAXs
1115   Node* y = r;
1116   jint y_off = 0;
1117   // Check final part of MAX tree
1118   if (y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant
1119       y->in(2)->is_Con()) {
1120     const Type* t = y->in(2)->bottom_type();
1121     if (t == Type::TOP) return NULL;  // No progress
1122     y_off = t->is_int()->get_con();
1123     y = y->in(1);
1124   }
1125   if (x->_idx > y->_idx && r->Opcode() != Op_MaxI) {
1126     swap_edges(1, 2);
1127     return this;
1128   }
1129 
1130   const TypeInt* tx = phase->type(x)->isa_int();
1131 
1132   if (r->Opcode() == Op_MaxI) {
1133     assert(r != r->in(2), "dead loop in MaxINode::Ideal");
1134     y = r->in(1);
1135     // Check final part of MAX tree
1136     if (y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant
1137         y->in(2)->is_Con()) {
1138       const Type* t = y->in(2)->bottom_type();
1139       if (t == Type::TOP) return NULL;  // No progress
1140       y_off = t->is_int()->get_con();
1141       y = y->in(1);
1142     }
1143 
1144     if (x->_idx > y->_idx)
1145       return new MaxINode(r->in(1), phase->transform(new MaxINode(l, r->in(2))));
1146 
1147     // Transform MAX2(x + c0, MAX2(x + c1, z)) into MAX2(x + MAX2(c0, c1), z)
1148     // if x == y and the additions can't overflow.
1149     if (x == y && tx != NULL &&
1150         !can_overflow(tx, x_off) &&
1151         !can_overflow(tx, y_off)) {
1152       return new MaxINode(phase->transform(new AddINode(x, phase->intcon(MAX2(x_off, y_off)))), r->in(2));
1153     }
1154   } else {
1155     // Transform MAX2(x + c0, y + c1) into x + MAX2(c0, c1)
1156     // if x == y and the additions can't overflow.
1157     if (x == y && tx != NULL &&
1158         !can_overflow(tx, x_off) &&
1159         !can_overflow(tx, y_off)) {
1160       return new AddINode(x, phase->intcon(MAX2(x_off, y_off)));
1161     }
1162   }
1163  return NULL;
1164 }
1165 
1166 //=============================================================================
1167 //------------------------------Idealize---------------------------------------
1168 // MINs show up in range-check loop limit calculations.  Look for
1169 // "MIN2(x+c0,MIN2(y,x+c1))".  Pick the smaller constant: "MIN2(x+c0,y)"
1170 Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) {
1171   Node *progress = NULL;
1172   // Force a right-spline graph
1173   Node *l = in(1);
1174   Node *r = in(2);
1175   // Transform  MinI1( MinI2(a,b), c)  into  MinI1( a, MinI2(b,c) )
1176   // to force a right-spline graph for the rest of MinINode::Ideal().
1177   if( l->Opcode() == Op_MinI ) {
1178     assert( l != l->in(1), "dead loop in MinINode::Ideal" );
1179     r = phase->transform(new MinINode(l->in(2),r));
1180     l = l->in(1);
1181     set_req_X(1, l, phase);
1182     set_req_X(2, r, phase);
1183     return this;
1184   }
1185 
1186   // Get left input & constant
1187   Node *x = l;
1188   jint x_off = 0;
1189   if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant
1190       x->in(2)->is_Con() ) {
1191     const Type *t = x->in(2)->bottom_type();
1192     if( t == Type::TOP ) return NULL;  // No progress
1193     x_off = t->is_int()->get_con();
1194     x = x->in(1);
1195   }
1196 
1197   // Scan a right-spline-tree for MINs
1198   Node *y = r;
1199   jint y_off = 0;
1200   // Check final part of MIN tree
1201   if( y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant
1202       y->in(2)->is_Con() ) {
1203     const Type *t = y->in(2)->bottom_type();
1204     if( t == Type::TOP ) return NULL;  // No progress
1205     y_off = t->is_int()->get_con();
1206     y = y->in(1);
1207   }
1208   if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) {
1209     swap_edges(1, 2);
1210     return this;
1211   }
1212 
1213   const TypeInt* tx = phase->type(x)->isa_int();
1214 
1215   if( r->Opcode() == Op_MinI ) {
1216     assert( r != r->in(2), "dead loop in MinINode::Ideal" );
1217     y = r->in(1);
1218     // Check final part of MIN tree
1219     if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant
1220         y->in(2)->is_Con() ) {
1221       const Type *t = y->in(2)->bottom_type();
1222       if( t == Type::TOP ) return NULL;  // No progress
1223       y_off = t->is_int()->get_con();
1224       y = y->in(1);
1225     }
1226 
1227     if( x->_idx > y->_idx )
1228       return new MinINode(r->in(1),phase->transform(new MinINode(l,r->in(2))));
1229 
1230     // Transform MIN2(x + c0, MIN2(x + c1, z)) into MIN2(x + MIN2(c0, c1), z)
1231     // if x == y and the additions can't overflow.
1232     if (x == y && tx != NULL &&
1233         !can_overflow(tx, x_off) &&
1234         !can_overflow(tx, y_off)) {
1235       return new MinINode(phase->transform(new AddINode(x, phase->intcon(MIN2(x_off, y_off)))), r->in(2));
1236     }
1237   } else {
1238     // Transform MIN2(x + c0, y + c1) into x + MIN2(c0, c1)
1239     // if x == y and the additions can't overflow.
1240     if (x == y && tx != NULL &&
1241         !can_overflow(tx, x_off) &&
1242         !can_overflow(tx, y_off)) {
1243       return new AddINode(x,phase->intcon(MIN2(x_off,y_off)));
1244     }
1245   }
1246   return NULL;
1247 }
1248 
1249 //------------------------------add_ring---------------------------------------
1250 // Supplied function returns the sum of the inputs.
1251 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
1252   const TypeInt *r0 = t0->is_int(); // Handy access
1253   const TypeInt *r1 = t1->is_int();
1254 
1255   // Otherwise just MIN them bits.
1256   return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1257 }
1258 
1259 //------------------------------add_ring---------------------------------------
1260 const Type *MinFNode::add_ring( const Type *t0, const Type *t1 ) const {
1261   const TypeF *r0 = t0->is_float_constant();
1262   const TypeF *r1 = t1->is_float_constant();
1263 
1264   if (r0->is_nan()) {
1265     return r0;
1266   }
1267   if (r1->is_nan()) {
1268     return r1;
1269   }
1270 
1271   float f0 = r0->getf();
1272   float f1 = r1->getf();
1273   if (f0 != 0.0f || f1 != 0.0f) {
1274     return f0 < f1 ? r0 : r1;
1275   }
1276 
1277   // handle min of 0.0, -0.0 case.
1278   return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1;
1279 }
1280 
1281 //------------------------------add_ring---------------------------------------
1282 const Type *MinDNode::add_ring( const Type *t0, const Type *t1 ) const {
1283   const TypeD *r0 = t0->is_double_constant();
1284   const TypeD *r1 = t1->is_double_constant();
1285 
1286   if (r0->is_nan()) {
1287     return r0;
1288   }
1289   if (r1->is_nan()) {
1290     return r1;
1291   }
1292 
1293   double d0 = r0->getd();
1294   double d1 = r1->getd();
1295   if (d0 != 0.0 || d1 != 0.0) {
1296     return d0 < d1 ? r0 : r1;
1297   }
1298 
1299   // handle min of 0.0, -0.0 case.
1300   return (jlong_cast(d0) < jlong_cast(d1)) ? r0 : r1;
1301 }
1302 
1303 //------------------------------add_ring---------------------------------------
1304 const Type *MaxFNode::add_ring( const Type *t0, const Type *t1 ) const {
1305   const TypeF *r0 = t0->is_float_constant();
1306   const TypeF *r1 = t1->is_float_constant();
1307 
1308   if (r0->is_nan()) {
1309     return r0;
1310   }
1311   if (r1->is_nan()) {
1312     return r1;
1313   }
1314 
1315   float f0 = r0->getf();
1316   float f1 = r1->getf();
1317   if (f0 != 0.0f || f1 != 0.0f) {
1318     return f0 > f1 ? r0 : r1;
1319   }
1320 
1321   // handle max of 0.0,-0.0 case.
1322   return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1;
1323 }
1324 
1325 //------------------------------add_ring---------------------------------------
1326 const Type *MaxDNode::add_ring( const Type *t0, const Type *t1 ) const {
1327   const TypeD *r0 = t0->is_double_constant();
1328   const TypeD *r1 = t1->is_double_constant();
1329 
1330   if (r0->is_nan()) {
1331     return r0;
1332   }
1333   if (r1->is_nan()) {
1334     return r1;
1335   }
1336 
1337   double d0 = r0->getd();
1338   double d1 = r1->getd();
1339   if (d0 != 0.0 || d1 != 0.0) {
1340     return d0 > d1 ? r0 : r1;
1341   }
1342 
1343   // handle max of 0.0, -0.0 case.
1344   return (jlong_cast(d0) > jlong_cast(d1)) ? r0 : r1;
1345 }