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