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