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   if (tp->isa_aryptr()) {
 771     // In the case of a flattened inline type array, each field has its
 772     // own slice so we need to extract the field being accessed from
 773     // the address computation
 774     return tp->is_aryptr()->add_field_offset_and_offset(txoffset);
 775   }
 776   return tp->add_offset(txoffset);
 777 }
 778 
 779 //------------------------------Value------------------------------------------
 780 const Type* AddPNode::Value(PhaseGVN* phase) const {
 781   // Either input is TOP ==> the result is TOP
 782   const Type *t1 = phase->type( in(Address) );
 783   const Type *t2 = phase->type( in(Offset) );
 784   if( t1 == Type::TOP ) return Type::TOP;
 785   if( t2 == Type::TOP ) return Type::TOP;
 786 
 787   // Left input is a pointer
 788   const TypePtr *p1 = t1->isa_ptr();
 789   // Right input is an int
 790   const TypeX *p2 = t2->is_intptr_t();
 791   // Add 'em
 792   intptr_t p2offset = Type::OffsetBot;
 793   if (p2->is_con()) {   // Left input is an add of a constant?
 794     p2offset = p2->get_con();
 795   }
 796   if (p1->isa_aryptr()) {
 797     // In the case of a flattened inline type array, each field has its
 798     // own slice so we need to extract the field being accessed from
 799     // the address computation
 800     return p1->is_aryptr()->add_field_offset_and_offset(p2offset);
 801   }
 802   return p1->add_offset(p2offset);
 803 }
 804 
 805 //------------------------Ideal_base_and_offset--------------------------------
 806 // Split an oop pointer into a base and offset.
 807 // (The offset might be Type::OffsetBot in the case of an array.)
 808 // Return the base, or NULL if failure.
 809 Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseTransform* phase,
 810                                       // second return value:
 811                                       intptr_t& offset) {
 812   if (ptr->is_AddP()) {
 813     Node* base = ptr->in(AddPNode::Base);
 814     Node* addr = ptr->in(AddPNode::Address);
 815     Node* offs = ptr->in(AddPNode::Offset);
 816     if (base == addr || base->is_top()) {
 817       offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
 818       if (offset != Type::OffsetBot) {
 819         return addr;
 820       }
 821     }
 822   }
 823   offset = Type::OffsetBot;
 824   return NULL;
 825 }
 826 
 827 //------------------------------unpack_offsets----------------------------------
 828 // Collect the AddP offset values into the elements array, giving up
 829 // if there are more than length.
 830 int AddPNode::unpack_offsets(Node* elements[], int length) {
 831   int count = 0;
 832   Node* addr = this;
 833   Node* base = addr->in(AddPNode::Base);
 834   while (addr->is_AddP()) {
 835     if (addr->in(AddPNode::Base) != base) {
 836       // give up
 837       return -1;
 838     }
 839     elements[count++] = addr->in(AddPNode::Offset);
 840     if (count == length) {
 841       // give up
 842       return -1;
 843     }
 844     addr = addr->in(AddPNode::Address);
 845   }
 846   if (addr != base) {
 847     return -1;
 848   }
 849   return count;
 850 }
 851 
 852 //------------------------------match_edge-------------------------------------
 853 // Do we Match on this edge index or not?  Do not match base pointer edge
 854 uint AddPNode::match_edge(uint idx) const {
 855   return idx > Base;
 856 }
 857 
 858 //=============================================================================
 859 //------------------------------Identity---------------------------------------
 860 Node* OrINode::Identity(PhaseGVN* phase) {
 861   // x | x => x
 862   if (in(1) == in(2)) {
 863     return in(1);
 864   }
 865 
 866   return AddNode::Identity(phase);
 867 }
 868 
 869 // Find shift value for Integer or Long OR.
 870 Node* rotate_shift(PhaseGVN* phase, Node* lshift, Node* rshift, int mask) {
 871   // val << norm_con_shift | val >> ({32|64} - norm_con_shift) => rotate_left val, norm_con_shift
 872   const TypeInt* lshift_t = phase->type(lshift)->isa_int();
 873   const TypeInt* rshift_t = phase->type(rshift)->isa_int();
 874   if (lshift_t != NULL && lshift_t->is_con() &&
 875       rshift_t != NULL && rshift_t->is_con() &&
 876       ((lshift_t->get_con() & mask) == ((mask + 1) - (rshift_t->get_con() & mask)))) {
 877     return phase->intcon(lshift_t->get_con() & mask);
 878   }
 879   // val << var_shift | val >> ({0|32|64} - var_shift) => rotate_left val, var_shift
 880   if (rshift->Opcode() == Op_SubI && rshift->in(2) == lshift && rshift->in(1)->is_Con()){
 881     const TypeInt* shift_t = phase->type(rshift->in(1))->isa_int();
 882     if (shift_t != NULL && shift_t->is_con() &&
 883         (shift_t->get_con() == 0 || shift_t->get_con() == (mask + 1))) {
 884       return lshift;
 885     }
 886   }
 887   return NULL;
 888 }
 889 
 890 Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 891   int lopcode = in(1)->Opcode();
 892   int ropcode = in(2)->Opcode();
 893   if (Matcher::match_rule_supported(Op_RotateLeft) &&
 894       lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) {
 895     Node* lshift = in(1)->in(2);
 896     Node* rshift = in(2)->in(2);
 897     Node* shift = rotate_shift(phase, lshift, rshift, 0x1F);
 898     if (shift != NULL) {
 899       return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT);
 900     }
 901     return NULL;
 902   }
 903   if (Matcher::match_rule_supported(Op_RotateRight) &&
 904       lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) {
 905     Node* rshift = in(1)->in(2);
 906     Node* lshift = in(2)->in(2);
 907     Node* shift = rotate_shift(phase, rshift, lshift, 0x1F);
 908     if (shift != NULL) {
 909       return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
 910     }
 911   }
 912   return NULL;
 913 }
 914 
 915 //------------------------------add_ring---------------------------------------
 916 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
 917 // the logical operations the ring's ADD is really a logical OR function.
 918 // This also type-checks the inputs for sanity.  Guaranteed never to
 919 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 920 const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
 921   const TypeInt *r0 = t0->is_int(); // Handy access
 922   const TypeInt *r1 = t1->is_int();
 923 
 924   // If both args are bool, can figure out better types
 925   if ( r0 == TypeInt::BOOL ) {
 926     if ( r1 == TypeInt::ONE) {
 927       return TypeInt::ONE;
 928     } else if ( r1 == TypeInt::BOOL ) {
 929       return TypeInt::BOOL;
 930     }
 931   } else if ( r0 == TypeInt::ONE ) {
 932     if ( r1 == TypeInt::BOOL ) {
 933       return TypeInt::ONE;
 934     }
 935   }
 936 
 937   // If either input is not a constant, just return all integers.
 938   if( !r0->is_con() || !r1->is_con() )
 939     return TypeInt::INT;        // Any integer, but still no symbols.
 940 
 941   // Otherwise just OR them bits.
 942   return TypeInt::make( r0->get_con() | r1->get_con() );
 943 }
 944 
 945 //=============================================================================
 946 //------------------------------Identity---------------------------------------
 947 Node* OrLNode::Identity(PhaseGVN* phase) {
 948   // x | x => x
 949   if (in(1) == in(2)) {
 950     return in(1);
 951   }
 952 
 953   return AddNode::Identity(phase);
 954 }
 955 
 956 Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 957   int lopcode = in(1)->Opcode();
 958   int ropcode = in(2)->Opcode();
 959   if (Matcher::match_rule_supported(Op_RotateLeft) &&
 960       lopcode == Op_LShiftL && ropcode == Op_URShiftL && in(1)->in(1) == in(2)->in(1)) {
 961     Node* lshift = in(1)->in(2);
 962     Node* rshift = in(2)->in(2);
 963     Node* shift = rotate_shift(phase, lshift, rshift, 0x3F);
 964     if (shift != NULL) {
 965       return new RotateLeftNode(in(1)->in(1), shift, TypeLong::LONG);
 966     }
 967     return NULL;
 968   }
 969   if (Matcher::match_rule_supported(Op_RotateRight) &&
 970       lopcode == Op_URShiftL && ropcode == Op_LShiftL && in(1)->in(1) == in(2)->in(1)) {
 971     Node* rshift = in(1)->in(2);
 972     Node* lshift = in(2)->in(2);
 973     Node* shift = rotate_shift(phase, rshift, lshift, 0x3F);
 974     if (shift != NULL) {
 975       return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG);
 976     }
 977   }
 978   return NULL;
 979 }
 980 
 981 //------------------------------add_ring---------------------------------------
 982 const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
 983   const TypeLong *r0 = t0->is_long(); // Handy access
 984   const TypeLong *r1 = t1->is_long();
 985 
 986   // If either input is not a constant, just return all integers.
 987   if( !r0->is_con() || !r1->is_con() )
 988     return TypeLong::LONG;      // Any integer, but still no symbols.
 989 
 990   // Otherwise just OR them bits.
 991   return TypeLong::make( r0->get_con() | r1->get_con() );
 992 }
 993 
 994 //=============================================================================
 995 //------------------------------Idealize---------------------------------------
 996 Node* XorINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 997   Node* in1 = in(1);
 998   Node* in2 = in(2);
 999   int op1 = in1->Opcode();
1000   // Convert ~(x-1) into -x. Note there isn't a bitwise not bytecode,
1001   // "~x" would typically represented as "x^(-1)", and "x-c0" would
1002   // convert into "x+ -c0" in SubXNode::Ideal. So ~(x-1) will eventually
1003   // be (x+(-1))^-1.
1004   if (op1 == Op_AddI && phase->type(in2) == TypeInt::MINUS_1 &&
1005       phase->type(in1->in(2)) == TypeInt::MINUS_1) {
1006     return new SubINode(phase->makecon(TypeInt::ZERO), in1->in(1));
1007   }
1008   return AddNode::Ideal(phase, can_reshape);
1009 }
1010 
1011 const Type* XorINode::Value(PhaseGVN* phase) const {
1012   Node* in1 = in(1);
1013   Node* in2 = in(2);
1014   const Type* t1 = phase->type(in1);
1015   const Type* t2 = phase->type(in2);
1016   if (t1 == Type::TOP || t2 == Type::TOP) {
1017     return Type::TOP;
1018   }
1019   // x ^ x ==> 0
1020   if (in1->eqv_uncast(in2)) {
1021     return add_id();
1022   }
1023   // result of xor can only have bits sets where any of the
1024   // inputs have bits set. lo can always become 0.
1025   const TypeInt* t1i = t1->is_int();
1026   const TypeInt* t2i = t2->is_int();
1027   if ((t1i->_lo >= 0) &&
1028       (t1i->_hi > 0)  &&
1029       (t2i->_lo >= 0) &&
1030       (t2i->_hi > 0)) {
1031     // hi - set all bits below the highest bit. Using round_down to avoid overflow.
1032     const TypeInt* t1x = TypeInt::make(0, round_down_power_of_2(t1i->_hi) + (round_down_power_of_2(t1i->_hi) - 1), t1i->_widen);
1033     const TypeInt* t2x = TypeInt::make(0, round_down_power_of_2(t2i->_hi) + (round_down_power_of_2(t2i->_hi) - 1), t2i->_widen);
1034     return t1x->meet(t2x);
1035   }
1036   return AddNode::Value(phase);
1037 }
1038 
1039 
1040 //------------------------------add_ring---------------------------------------
1041 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
1042 // the logical operations the ring's ADD is really a logical OR function.
1043 // This also type-checks the inputs for sanity.  Guaranteed never to
1044 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
1045 const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
1046   const TypeInt *r0 = t0->is_int(); // Handy access
1047   const TypeInt *r1 = t1->is_int();
1048 
1049   // Complementing a boolean?
1050   if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE
1051                                || r1 == TypeInt::BOOL))
1052     return TypeInt::BOOL;
1053 
1054   if( !r0->is_con() || !r1->is_con() ) // Not constants
1055     return TypeInt::INT;        // Any integer, but still no symbols.
1056 
1057   // Otherwise just XOR them bits.
1058   return TypeInt::make( r0->get_con() ^ r1->get_con() );
1059 }
1060 
1061 //=============================================================================
1062 //------------------------------add_ring---------------------------------------
1063 const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
1064   const TypeLong *r0 = t0->is_long(); // Handy access
1065   const TypeLong *r1 = t1->is_long();
1066 
1067   // If either input is not a constant, just return all integers.
1068   if( !r0->is_con() || !r1->is_con() )
1069     return TypeLong::LONG;      // Any integer, but still no symbols.
1070 
1071   // Otherwise just OR them bits.
1072   return TypeLong::make( r0->get_con() ^ r1->get_con() );
1073 }
1074 
1075 Node* XorLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1076   Node* in1 = in(1);
1077   Node* in2 = in(2);
1078   int op1 = in1->Opcode();
1079   // Convert ~(x-1) into -x. Note there isn't a bitwise not bytecode,
1080   // "~x" would typically represented as "x^(-1)", and "x-c0" would
1081   // convert into "x+ -c0" in SubXNode::Ideal. So ~(x-1) will eventually
1082   // be (x+(-1))^-1.
1083   if (op1 == Op_AddL && phase->type(in2) == TypeLong::MINUS_1 &&
1084       phase->type(in1->in(2)) == TypeLong::MINUS_1) {
1085     return new SubLNode(phase->makecon(TypeLong::ZERO), in1->in(1));
1086   }
1087   return AddNode::Ideal(phase, can_reshape);
1088 }
1089 
1090 const Type* XorLNode::Value(PhaseGVN* phase) const {
1091   Node* in1 = in(1);
1092   Node* in2 = in(2);
1093   const Type* t1 = phase->type(in1);
1094   const Type* t2 = phase->type(in2);
1095   if (t1 == Type::TOP || t2 == Type::TOP) {
1096     return Type::TOP;
1097   }
1098   // x ^ x ==> 0
1099   if (in1->eqv_uncast(in2)) {
1100     return add_id();
1101   }
1102   // result of xor can only have bits sets where any of the
1103   // inputs have bits set. lo can always become 0.
1104   const TypeLong* t1l = t1->is_long();
1105   const TypeLong* t2l = t2->is_long();
1106   if ((t1l->_lo >= 0) &&
1107       (t1l->_hi > 0)  &&
1108       (t2l->_lo >= 0) &&
1109       (t2l->_hi > 0)) {
1110     // hi - set all bits below the highest bit. Using round_down to avoid overflow.
1111     const TypeLong* t1x = TypeLong::make(0, round_down_power_of_2(t1l->_hi) + (round_down_power_of_2(t1l->_hi) - 1), t1l->_widen);
1112     const TypeLong* t2x = TypeLong::make(0, round_down_power_of_2(t2l->_hi) + (round_down_power_of_2(t2l->_hi) - 1), t2l->_widen);
1113     return t1x->meet(t2x);
1114   }
1115   return AddNode::Value(phase);
1116 }
1117 
1118 Node* MaxNode::build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn) {
1119   bool is_int = gvn.type(a)->isa_int();
1120   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1121   assert(is_int == (gvn.type(b)->isa_int() != NULL), "inconsistent inputs");
1122   Node* hook = NULL;
1123   if (gvn.is_IterGVN()) {
1124     // Make sure a and b are not destroyed
1125     hook = new Node(2);
1126     hook->init_req(0, a);
1127     hook->init_req(1, b);
1128   }
1129   Node* res = NULL;
1130   if (!is_unsigned) {
1131     if (is_max) {
1132       if (is_int) {
1133         res =  gvn.transform(new MaxINode(a, b));
1134         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");
1135       } else {
1136         Node* cmp = gvn.transform(new CmpLNode(a, b));
1137         Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1138         res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
1139       }
1140     } else {
1141       if (is_int) {
1142         Node* res =  gvn.transform(new MinINode(a, b));
1143         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");
1144       } else {
1145         Node* cmp = gvn.transform(new CmpLNode(b, a));
1146         Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1147         res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
1148       }
1149     }
1150   } else {
1151     if (is_max) {
1152       if (is_int) {
1153         Node* cmp = gvn.transform(new CmpUNode(a, b));
1154         Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1155         res = gvn.transform(new CMoveINode(bol, a, b, t->is_int()));
1156       } else {
1157         Node* cmp = gvn.transform(new CmpULNode(a, b));
1158         Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1159         res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
1160       }
1161     } else {
1162       if (is_int) {
1163         Node* cmp = gvn.transform(new CmpUNode(b, a));
1164         Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1165         res = gvn.transform(new CMoveINode(bol, a, b, t->is_int()));
1166       } else {
1167         Node* cmp = gvn.transform(new CmpULNode(b, a));
1168         Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1169         res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
1170       }
1171     }
1172   }
1173   if (hook != NULL) {
1174     hook->destruct(&gvn);
1175   }
1176   return res;
1177 }
1178 
1179 Node* MaxNode::build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn) {
1180   bool is_int = gvn.type(a)->isa_int();
1181   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1182   assert(is_int == (gvn.type(b)->isa_int() != NULL), "inconsistent inputs");
1183   Node* zero = NULL;
1184   if (is_int) {
1185     zero = gvn.intcon(0);
1186   } else {
1187     zero = gvn.longcon(0);
1188   }
1189   Node* hook = NULL;
1190   if (gvn.is_IterGVN()) {
1191     // Make sure a and b are not destroyed
1192     hook = new Node(2);
1193     hook->init_req(0, a);
1194     hook->init_req(1, b);
1195   }
1196   Node* res = NULL;
1197   if (is_max) {
1198     if (is_int) {
1199       Node* cmp = gvn.transform(new CmpINode(a, b));
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(a, b));
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   } else {
1210     if (is_int) {
1211       Node* cmp = gvn.transform(new CmpINode(b, a));
1212       Node* sub = gvn.transform(new SubINode(a, b));
1213       Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1214       res = gvn.transform(new CMoveINode(bol, sub, zero, t->is_int()));
1215     } else {
1216       Node* cmp = gvn.transform(new CmpLNode(b, a));
1217       Node* sub = gvn.transform(new SubLNode(a, b));
1218       Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1219       res = gvn.transform(new CMoveLNode(bol, sub, zero, t->is_long()));
1220     }
1221   }
1222   if (hook != NULL) {
1223     hook->destruct(&gvn);
1224   }
1225   return res;
1226 }
1227 
1228 //=============================================================================
1229 //------------------------------add_ring---------------------------------------
1230 // Supplied function returns the sum of the inputs.
1231 const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
1232   const TypeInt *r0 = t0->is_int(); // Handy access
1233   const TypeInt *r1 = t1->is_int();
1234 
1235   // Otherwise just MAX them bits.
1236   return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1237 }
1238 
1239 // Check if addition of an integer with type 't' and a constant 'c' can overflow
1240 static bool can_overflow(const TypeInt* t, jint c) {
1241   jint t_lo = t->_lo;
1242   jint t_hi = t->_hi;
1243   return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1244           (c > 0 && (java_add(t_hi, c) < t_hi)));
1245 }
1246 
1247 //=============================================================================
1248 //------------------------------Idealize---------------------------------------
1249 // MINs show up in range-check loop limit calculations.  Look for
1250 // "MIN2(x+c0,MIN2(y,x+c1))".  Pick the smaller constant: "MIN2(x+c0,y)"
1251 Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) {
1252   Node *progress = NULL;
1253   // Force a right-spline graph
1254   Node *l = in(1);
1255   Node *r = in(2);
1256   // Transform  MinI1( MinI2(a,b), c)  into  MinI1( a, MinI2(b,c) )
1257   // to force a right-spline graph for the rest of MinINode::Ideal().
1258   if( l->Opcode() == Op_MinI ) {
1259     assert( l != l->in(1), "dead loop in MinINode::Ideal" );
1260     r = phase->transform(new MinINode(l->in(2),r));
1261     l = l->in(1);
1262     set_req_X(1, l, phase);
1263     set_req_X(2, r, phase);
1264     return this;
1265   }
1266 
1267   // Get left input & constant
1268   Node *x = l;
1269   jint x_off = 0;
1270   if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant
1271       x->in(2)->is_Con() ) {
1272     const Type *t = x->in(2)->bottom_type();
1273     if( t == Type::TOP ) return NULL;  // No progress
1274     x_off = t->is_int()->get_con();
1275     x = x->in(1);
1276   }
1277 
1278   // Scan a right-spline-tree for MINs
1279   Node *y = r;
1280   jint y_off = 0;
1281   // Check final part of MIN tree
1282   if( y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant
1283       y->in(2)->is_Con() ) {
1284     const Type *t = y->in(2)->bottom_type();
1285     if( t == Type::TOP ) return NULL;  // No progress
1286     y_off = t->is_int()->get_con();
1287     y = y->in(1);
1288   }
1289   if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) {
1290     swap_edges(1, 2);
1291     return this;
1292   }
1293 
1294   const TypeInt* tx = phase->type(x)->isa_int();
1295 
1296   if( r->Opcode() == Op_MinI ) {
1297     assert( r != r->in(2), "dead loop in MinINode::Ideal" );
1298     y = r->in(1);
1299     // Check final part of MIN tree
1300     if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant
1301         y->in(2)->is_Con() ) {
1302       const Type *t = y->in(2)->bottom_type();
1303       if( t == Type::TOP ) return NULL;  // No progress
1304       y_off = t->is_int()->get_con();
1305       y = y->in(1);
1306     }
1307 
1308     if( x->_idx > y->_idx )
1309       return new MinINode(r->in(1),phase->transform(new MinINode(l,r->in(2))));
1310 
1311     // Transform MIN2(x + c0, MIN2(x + c1, z)) into MIN2(x + MIN2(c0, c1), z)
1312     // if x == y and the additions can't overflow.
1313     if (x == y && tx != NULL &&
1314         !can_overflow(tx, x_off) &&
1315         !can_overflow(tx, y_off)) {
1316       return new MinINode(phase->transform(new AddINode(x, phase->intcon(MIN2(x_off, y_off)))), r->in(2));
1317     }
1318   } else {
1319     // Transform MIN2(x + c0, y + c1) into x + MIN2(c0, c1)
1320     // if x == y and the additions can't overflow.
1321     if (x == y && tx != NULL &&
1322         !can_overflow(tx, x_off) &&
1323         !can_overflow(tx, y_off)) {
1324       return new AddINode(x,phase->intcon(MIN2(x_off,y_off)));
1325     }
1326   }
1327   return NULL;
1328 }
1329 
1330 //------------------------------add_ring---------------------------------------
1331 // Supplied function returns the sum of the inputs.
1332 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
1333   const TypeInt *r0 = t0->is_int(); // Handy access
1334   const TypeInt *r1 = t1->is_int();
1335 
1336   // Otherwise just MIN them bits.
1337   return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1338 }
1339 
1340 //------------------------------add_ring---------------------------------------
1341 const Type *MinFNode::add_ring( const Type *t0, const Type *t1 ) const {
1342   const TypeF *r0 = t0->is_float_constant();
1343   const TypeF *r1 = t1->is_float_constant();
1344 
1345   if (r0->is_nan()) {
1346     return r0;
1347   }
1348   if (r1->is_nan()) {
1349     return r1;
1350   }
1351 
1352   float f0 = r0->getf();
1353   float f1 = r1->getf();
1354   if (f0 != 0.0f || f1 != 0.0f) {
1355     return f0 < f1 ? r0 : r1;
1356   }
1357 
1358   // handle min of 0.0, -0.0 case.
1359   return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1;
1360 }
1361 
1362 //------------------------------add_ring---------------------------------------
1363 const Type *MinDNode::add_ring( const Type *t0, const Type *t1 ) const {
1364   const TypeD *r0 = t0->is_double_constant();
1365   const TypeD *r1 = t1->is_double_constant();
1366 
1367   if (r0->is_nan()) {
1368     return r0;
1369   }
1370   if (r1->is_nan()) {
1371     return r1;
1372   }
1373 
1374   double d0 = r0->getd();
1375   double d1 = r1->getd();
1376   if (d0 != 0.0 || d1 != 0.0) {
1377     return d0 < d1 ? r0 : r1;
1378   }
1379 
1380   // handle min of 0.0, -0.0 case.
1381   return (jlong_cast(d0) < jlong_cast(d1)) ? r0 : r1;
1382 }
1383 
1384 //------------------------------add_ring---------------------------------------
1385 const Type *MaxFNode::add_ring( const Type *t0, const Type *t1 ) const {
1386   const TypeF *r0 = t0->is_float_constant();
1387   const TypeF *r1 = t1->is_float_constant();
1388 
1389   if (r0->is_nan()) {
1390     return r0;
1391   }
1392   if (r1->is_nan()) {
1393     return r1;
1394   }
1395 
1396   float f0 = r0->getf();
1397   float f1 = r1->getf();
1398   if (f0 != 0.0f || f1 != 0.0f) {
1399     return f0 > f1 ? r0 : r1;
1400   }
1401 
1402   // handle max of 0.0,-0.0 case.
1403   return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1;
1404 }
1405 
1406 //------------------------------add_ring---------------------------------------
1407 const Type *MaxDNode::add_ring( const Type *t0, const Type *t1 ) const {
1408   const TypeD *r0 = t0->is_double_constant();
1409   const TypeD *r1 = t1->is_double_constant();
1410 
1411   if (r0->is_nan()) {
1412     return r0;
1413   }
1414   if (r1->is_nan()) {
1415     return r1;
1416   }
1417 
1418   double d0 = r0->getd();
1419   double d1 = r1->getd();
1420   if (d0 != 0.0 || d1 != 0.0) {
1421     return d0 > d1 ? r0 : r1;
1422   }
1423 
1424   // handle max of 0.0, -0.0 case.
1425   return (jlong_cast(d0) > jlong_cast(d1)) ? r0 : r1;
1426 }