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