1 /*
   2  * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "memory/allocation.inline.hpp"
  27 #include "opto/addnode.hpp"
  28 #include "opto/castnode.hpp"
  29 #include "opto/cfgnode.hpp"
  30 #include "opto/connode.hpp"
  31 #include "opto/machnode.hpp"
  32 #include "opto/movenode.hpp"
  33 #include "opto/mulnode.hpp"
  34 #include "opto/phaseX.hpp"
  35 #include "opto/subnode.hpp"
  36 
  37 // Portions of code courtesy of Clifford Click
  38 
  39 // Classic Add functionality.  This covers all the usual 'add' behaviors for
  40 // an algebraic ring.  Add-integer, add-float, add-double, and binary-or are
  41 // all inherited from this class.  The various identity values are supplied
  42 // by virtual functions.
  43 
  44 
  45 //=============================================================================
  46 //------------------------------hash-------------------------------------------
  47 // Hash function over AddNodes.  Needs to be commutative; i.e., I swap
  48 // (commute) inputs to AddNodes willy-nilly so the hash function must return
  49 // the same value in the presence of edge swapping.
  50 uint AddNode::hash() const {
  51   return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();
  52 }
  53 
  54 //------------------------------Identity---------------------------------------
  55 // If either input is a constant 0, return the other input.
  56 Node* AddNode::Identity(PhaseGVN* phase) {
  57   const Type *zero = add_id();  // The additive identity
  58   if( phase->type( in(1) )->higher_equal( zero ) ) return in(2);
  59   if( phase->type( in(2) )->higher_equal( zero ) ) return in(1);
  60   return this;
  61 }
  62 
  63 //------------------------------commute----------------------------------------
  64 // Commute operands to move loads and constants to the right.
  65 static bool commute(PhaseGVN* phase, Node* add) {
  66   Node *in1 = add->in(1);
  67   Node *in2 = add->in(2);
  68 
  69   // convert "max(a,b) + min(a,b)" into "a+b".
  70   if ((in1->Opcode() == add->as_Add()->max_opcode() && in2->Opcode() == add->as_Add()->min_opcode())
  71       || (in1->Opcode() == add->as_Add()->min_opcode() && in2->Opcode() == add->as_Add()->max_opcode())) {
  72     Node *in11 = in1->in(1);
  73     Node *in12 = in1->in(2);
  74 
  75     Node *in21 = in2->in(1);
  76     Node *in22 = in2->in(2);
  77 
  78     if ((in11 == in21 && in12 == in22) ||
  79         (in11 == in22 && in12 == in21)) {
  80       add->set_req_X(1, in11, phase);
  81       add->set_req_X(2, in12, phase);
  82       return true;
  83     }
  84   }
  85 
  86   bool con_left = phase->type(in1)->singleton();
  87   bool con_right = phase->type(in2)->singleton();
  88 
  89   // Convert "1+x" into "x+1".
  90   // Right is a constant; leave it
  91   if( con_right ) return false;
  92   // Left is a constant; move it right.
  93   if( con_left ) {
  94     add->swap_edges(1, 2);
  95     return true;
  96   }
  97 
  98   // Convert "Load+x" into "x+Load".
  99   // Now check for loads
 100   if (in2->is_Load()) {
 101     if (!in1->is_Load()) {
 102       // already x+Load to return
 103       return false;
 104     }
 105     // both are loads, so fall through to sort inputs by idx
 106   } else if( in1->is_Load() ) {
 107     // Left is a Load and Right is not; move it right.
 108     add->swap_edges(1, 2);
 109     return true;
 110   }
 111 
 112   PhiNode *phi;
 113   // Check for tight loop increments: Loop-phi of Add of loop-phi
 114   if (in1->is_Phi() && (phi = in1->as_Phi()) && phi->region()->is_Loop() && phi->in(2) == add)
 115     return false;
 116   if (in2->is_Phi() && (phi = in2->as_Phi()) && phi->region()->is_Loop() && phi->in(2) == add) {
 117     add->swap_edges(1, 2);
 118     return true;
 119   }
 120 
 121   // Otherwise, sort inputs (commutativity) to help value numbering.
 122   if( in1->_idx > in2->_idx ) {
 123     add->swap_edges(1, 2);
 124     return true;
 125   }
 126   return false;
 127 }
 128 
 129 //------------------------------Idealize---------------------------------------
 130 // If we get here, we assume we are associative!
 131 Node *AddNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 132   const Type *t1 = phase->type(in(1));
 133   const Type *t2 = phase->type(in(2));
 134   bool con_left  = t1->singleton();
 135   bool con_right = t2->singleton();
 136 
 137   // Check for commutative operation desired
 138   if (commute(phase, this)) return this;
 139 
 140   AddNode *progress = nullptr;             // Progress flag
 141 
 142   // Convert "(x+1)+2" into "x+(1+2)".  If the right input is a
 143   // constant, and the left input is an add of a constant, flatten the
 144   // expression tree.
 145   Node *add1 = in(1);
 146   Node *add2 = in(2);
 147   int add1_op = add1->Opcode();
 148   int this_op = Opcode();
 149   if (con_right && t2 != Type::TOP && // Right input is a constant?
 150       add1_op == this_op) { // Left input is an Add?
 151 
 152     // Type of left _in right input
 153     const Type *t12 = phase->type(add1->in(2));
 154     if (t12->singleton() && t12 != Type::TOP) { // Left input is an add of a constant?
 155       // Check for rare case of closed data cycle which can happen inside
 156       // unreachable loops. In these cases the computation is undefined.
 157 #ifdef ASSERT
 158       Node *add11    = add1->in(1);
 159       int   add11_op = add11->Opcode();
 160       if ((add1 == add1->in(1))
 161           || (add11_op == this_op && add11->in(1) == add1)) {
 162         assert(false, "dead loop in AddNode::Ideal");
 163       }
 164 #endif
 165       // The Add of the flattened expression
 166       Node *x1 = add1->in(1);
 167       Node *x2 = phase->makecon(add1->as_Add()->add_ring(t2, t12));
 168       set_req_X(2, x2, phase);
 169       set_req_X(1, x1, phase);
 170       progress = this;            // Made progress
 171       add1 = in(1);
 172       add1_op = add1->Opcode();
 173     }
 174   }
 175 
 176   // Convert "(x+1)+y" into "(x+y)+1".  Push constants down the expression tree.
 177   if (add1_op == this_op && !con_right) {
 178     Node *a12 = add1->in(2);
 179     const Type *t12 = phase->type( a12 );
 180     if (t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) &&
 181         !(add1->in(1)->is_Phi() && (add1->in(1)->as_Phi()->is_tripcount(T_INT) || add1->in(1)->as_Phi()->is_tripcount(T_LONG)))) {
 182       assert(add1->in(1) != this, "dead loop in AddNode::Ideal");
 183       add2 = add1->clone();
 184       add2->set_req(2, in(2));
 185       add2 = phase->transform(add2);
 186       set_req_X(1, add2, phase);
 187       set_req_X(2, a12, phase);
 188       progress = this;
 189       add2 = a12;
 190     }
 191   }
 192 
 193   // Convert "x+(y+1)" into "(x+y)+1".  Push constants down the expression tree.
 194   int add2_op = add2->Opcode();
 195   if (add2_op == this_op && !con_left) {
 196     Node *a22 = add2->in(2);
 197     const Type *t22 = phase->type( a22 );
 198     if (t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) &&
 199         !(add2->in(1)->is_Phi() && (add2->in(1)->as_Phi()->is_tripcount(T_INT) || add2->in(1)->as_Phi()->is_tripcount(T_LONG)))) {
 200       assert(add2->in(1) != this, "dead loop in AddNode::Ideal");
 201       Node *addx = add2->clone();
 202       addx->set_req(1, in(1));
 203       addx->set_req(2, add2->in(1));
 204       addx = phase->transform(addx);
 205       set_req_X(1, addx, phase);
 206       set_req_X(2, a22, phase);
 207       progress = this;
 208     }
 209   }
 210 
 211   return progress;
 212 }
 213 
 214 //------------------------------Value-----------------------------------------
 215 // An add node sums it's two _in.  If one input is an RSD, we must mixin
 216 // the other input's symbols.
 217 const Type* AddNode::Value(PhaseGVN* phase) const {
 218   // Either input is TOP ==> the result is TOP
 219   const Type* t1 = phase->type(in(1));
 220   const Type* t2 = phase->type(in(2));
 221   if (t1 == Type::TOP || t2 == Type::TOP) {
 222     return Type::TOP;
 223   }
 224 
 225   // Check for an addition involving the additive identity
 226   const Type* tadd = add_of_identity(t1, t2);
 227   if (tadd != nullptr) {
 228     return tadd;
 229   }
 230 
 231   return add_ring(t1, t2);               // Local flavor of type addition
 232 }
 233 
 234 //------------------------------add_identity-----------------------------------
 235 // Check for addition of the identity
 236 const Type *AddNode::add_of_identity( const Type *t1, const Type *t2 ) const {
 237   const Type *zero = add_id();  // The additive identity
 238   if( t1->higher_equal( zero ) ) return t2;
 239   if( t2->higher_equal( zero ) ) return t1;
 240 
 241   return nullptr;
 242 }
 243 
 244 AddNode* AddNode::make(Node* in1, Node* in2, BasicType bt) {
 245   switch (bt) {
 246     case T_INT:
 247       return new AddINode(in1, in2);
 248     case T_LONG:
 249       return new AddLNode(in1, in2);
 250     default:
 251       fatal("Not implemented for %s", type2name(bt));
 252   }
 253   return nullptr;
 254 }
 255 
 256 bool AddNode::is_not(PhaseGVN* phase, Node* n, BasicType bt) {
 257   return n->Opcode() == Op_Xor(bt) && phase->type(n->in(2)) == TypeInteger::minus_1(bt);
 258 }
 259 
 260 AddNode* AddNode::make_not(PhaseGVN* phase, Node* n, BasicType bt) {
 261   switch (bt) {
 262     case T_INT:
 263       return new XorINode(n, phase->intcon(-1));
 264     case T_LONG:
 265       return new XorLNode(n, phase->longcon(-1L));
 266     default:
 267       fatal("Not implemented for %s", type2name(bt));
 268   }
 269   return nullptr;
 270 }
 271 
 272 //=============================================================================
 273 //------------------------------Idealize---------------------------------------
 274 Node* AddNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) {
 275   Node* in1 = in(1);
 276   Node* in2 = in(2);
 277   int op1 = in1->Opcode();
 278   int op2 = in2->Opcode();
 279   // Fold (con1-x)+con2 into (con1+con2)-x
 280   if (op1 == Op_Add(bt) && op2 == Op_Sub(bt)) {
 281     // Swap edges to try optimizations below
 282     in1 = in2;
 283     in2 = in(1);
 284     op1 = op2;
 285     op2 = in2->Opcode();
 286   }
 287   if (op1 == Op_Sub(bt)) {
 288     const Type* t_sub1 = phase->type(in1->in(1));
 289     const Type* t_2    = phase->type(in2       );
 290     if (t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP) {
 291       return SubNode::make(phase->makecon(add_ring(t_sub1, t_2)), in1->in(2), bt);
 292     }
 293     // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
 294     if (op2 == Op_Sub(bt)) {
 295       // Check for dead cycle: d = (a-b)+(c-d)
 296       assert( in1->in(2) != this && in2->in(2) != this,
 297               "dead loop in AddINode::Ideal" );
 298       Node* sub = SubNode::make(nullptr, nullptr, bt);
 299       Node* sub_in1;
 300       PhaseIterGVN* igvn = phase->is_IterGVN();
 301       // During IGVN, if both inputs of the new AddNode are a tree of SubNodes, this same transformation will be applied
 302       // to every node of the tree. Calling transform() causes the transformation to be applied recursively, once per
 303       // tree node whether some subtrees are identical or not. Pushing to the IGVN worklist instead, causes the transform
 304       // to be applied once per unique subtrees (because all uses of a subtree are updated with the result of the
 305       // transformation). In case of a large tree, this can make a difference in compilation time.
 306       if (igvn != nullptr) {
 307         sub_in1 = igvn->register_new_node_with_optimizer(AddNode::make(in1->in(1), in2->in(1), bt));
 308       } else {
 309         sub_in1 = phase->transform(AddNode::make(in1->in(1), in2->in(1), bt));
 310       }
 311       Node* sub_in2;
 312       if (igvn != nullptr) {
 313         sub_in2 = igvn->register_new_node_with_optimizer(AddNode::make(in1->in(2), in2->in(2), bt));
 314       } else {
 315         sub_in2 = phase->transform(AddNode::make(in1->in(2), in2->in(2), bt));
 316       }
 317       sub->init_req(1, sub_in1);
 318       sub->init_req(2, sub_in2);
 319       return sub;
 320     }
 321     // Convert "(a-b)+(b+c)" into "(a+c)"
 322     if (op2 == Op_Add(bt) && in1->in(2) == in2->in(1)) {
 323       assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal/AddLNode::Ideal");
 324       return AddNode::make(in1->in(1), in2->in(2), bt);
 325     }
 326     // Convert "(a-b)+(c+b)" into "(a+c)"
 327     if (op2 == Op_Add(bt) && in1->in(2) == in2->in(2)) {
 328       assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal/AddLNode::Ideal");
 329       return AddNode::make(in1->in(1), in2->in(1), bt);
 330     }
 331   }
 332 
 333   // Convert (con - y) + x into "(x - y) + con"
 334   if (op1 == Op_Sub(bt) && in1->in(1)->Opcode() == Op_ConIL(bt)
 335       && in1 != in1->in(2) && !(in1->in(2)->is_Phi() && in1->in(2)->as_Phi()->is_tripcount(bt))) {
 336     return AddNode::make(phase->transform(SubNode::make(in2, in1->in(2), bt)), in1->in(1), bt);
 337   }
 338 
 339   // Convert x + (con - y) into "(x - y) + con"
 340   if (op2 == Op_Sub(bt) && in2->in(1)->Opcode() == Op_ConIL(bt)
 341       && in2 != in2->in(2) && !(in2->in(2)->is_Phi() && in2->in(2)->as_Phi()->is_tripcount(bt))) {
 342     return AddNode::make(phase->transform(SubNode::make(in1, in2->in(2), bt)), in2->in(1), bt);
 343   }
 344 
 345   // Associative
 346   if (op1 == Op_Mul(bt) && op2 == Op_Mul(bt)) {
 347     Node* add_in1 = nullptr;
 348     Node* add_in2 = nullptr;
 349     Node* mul_in = nullptr;
 350 
 351     if (in1->in(1) == in2->in(1)) {
 352       // Convert "a*b+a*c into a*(b+c)
 353       add_in1 = in1->in(2);
 354       add_in2 = in2->in(2);
 355       mul_in = in1->in(1);
 356     } else if (in1->in(2) == in2->in(1)) {
 357       // Convert a*b+b*c into b*(a+c)
 358       add_in1 = in1->in(1);
 359       add_in2 = in2->in(2);
 360       mul_in = in1->in(2);
 361     } else if (in1->in(2) == in2->in(2)) {
 362       // Convert a*c+b*c into (a+b)*c
 363       add_in1 = in1->in(1);
 364       add_in2 = in2->in(1);
 365       mul_in = in1->in(2);
 366     } else if (in1->in(1) == in2->in(2)) {
 367       // Convert a*b+c*a into a*(b+c)
 368       add_in1 = in1->in(2);
 369       add_in2 = in2->in(1);
 370       mul_in = in1->in(1);
 371     }
 372 
 373     if (mul_in != nullptr) {
 374       Node* add = phase->transform(AddNode::make(add_in1, add_in2, bt));
 375       return MulNode::make(mul_in, add, bt);
 376     }
 377   }
 378 
 379   // Convert (x >>> rshift) + (x << lshift) into RotateRight(x, rshift)
 380   if (Matcher::match_rule_supported(Op_RotateRight) &&
 381       ((op1 == Op_URShift(bt) && op2 == Op_LShift(bt)) || (op1 == Op_LShift(bt) && op2 == Op_URShift(bt))) &&
 382       in1->in(1) != nullptr && in1->in(1) == in2->in(1)) {
 383     Node* rshift = op1 == Op_URShift(bt) ? in1->in(2) : in2->in(2);
 384     Node* lshift = op1 == Op_URShift(bt) ? in2->in(2) : in1->in(2);
 385     if (rshift != nullptr && lshift != nullptr) {
 386       const TypeInt* rshift_t = phase->type(rshift)->isa_int();
 387       const TypeInt* lshift_t = phase->type(lshift)->isa_int();
 388       int bits = bt == T_INT ? 32 : 64;
 389       int mask = bt == T_INT ? 0x1F : 0x3F;
 390       if (lshift_t != nullptr && lshift_t->is_con() &&
 391           rshift_t != nullptr && rshift_t->is_con() &&
 392           ((lshift_t->get_con() & mask) == (bits - (rshift_t->get_con() & mask)))) {
 393         return new RotateRightNode(in1->in(1), phase->intcon(rshift_t->get_con() & mask), TypeInteger::bottom(bt));
 394       }
 395     }
 396   }
 397 
 398   return AddNode::Ideal(phase, can_reshape);
 399 }
 400 
 401 
 402 Node* AddINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 403   Node* in1 = in(1);
 404   Node* in2 = in(2);
 405   int op1 = in1->Opcode();
 406   int op2 = in2->Opcode();
 407 
 408   // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
 409   // Helps with array allocation math constant folding
 410   // See 4790063:
 411   // Unrestricted transformation is unsafe for some runtime values of 'x'
 412   // ( x ==  0, z == 1, y == -1 ) fails
 413   // ( x == -5, z == 1, y ==  1 ) fails
 414   // Transform works for small z and small negative y when the addition
 415   // (x + (y << z)) does not cross zero.
 416   // Implement support for negative y and (x >= -(y << z))
 417   // Have not observed cases where type information exists to support
 418   // positive y and (x <= -(y << z))
 419   if (op1 == Op_URShiftI && op2 == Op_ConI &&
 420       in1->in(2)->Opcode() == Op_ConI) {
 421     jint z = phase->type(in1->in(2))->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
 422     jint y = phase->type(in2)->is_int()->get_con();
 423 
 424     if (z < 5 && -5 < y && y < 0) {
 425       const Type* t_in11 = phase->type(in1->in(1));
 426       if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z))) {
 427         Node* a = phase->transform(new AddINode( in1->in(1), phase->intcon(y<<z)));
 428         return new URShiftINode(a, in1->in(2));
 429       }
 430     }
 431   }
 432 
 433   return AddNode::IdealIL(phase, can_reshape, T_INT);
 434 }
 435 
 436 
 437 //------------------------------Identity---------------------------------------
 438 // Fold (x-y)+y  OR  y+(x-y)  into  x
 439 Node* AddINode::Identity(PhaseGVN* phase) {
 440   if (in(1)->Opcode() == Op_SubI && in(1)->in(2) == in(2)) {
 441     return in(1)->in(1);
 442   } else if (in(2)->Opcode() == Op_SubI && in(2)->in(2) == in(1)) {
 443     return in(2)->in(1);
 444   }
 445   return AddNode::Identity(phase);
 446 }
 447 
 448 
 449 //------------------------------add_ring---------------------------------------
 450 // Supplied function returns the sum of the inputs.  Guaranteed never
 451 // to be passed a TOP or BOTTOM type, these are filtered out by
 452 // pre-check.
 453 const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {
 454   const TypeInt *r0 = t0->is_int(); // Handy access
 455   const TypeInt *r1 = t1->is_int();
 456   int lo = java_add(r0->_lo, r1->_lo);
 457   int hi = java_add(r0->_hi, r1->_hi);
 458   if( !(r0->is_con() && r1->is_con()) ) {
 459     // Not both constants, compute approximate result
 460     if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
 461       lo = min_jint; hi = max_jint; // Underflow on the low side
 462     }
 463     if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
 464       lo = min_jint; hi = max_jint; // Overflow on the high side
 465     }
 466     if( lo > hi ) {               // Handle overflow
 467       lo = min_jint; hi = max_jint;
 468     }
 469   } else {
 470     // both constants, compute precise result using 'lo' and 'hi'
 471     // Semantics define overflow and underflow for integer addition
 472     // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
 473   }
 474   return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
 475 }
 476 
 477 
 478 //=============================================================================
 479 //------------------------------Idealize---------------------------------------
 480 Node* AddLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 481   return AddNode::IdealIL(phase, can_reshape, T_LONG);
 482 }
 483 
 484 
 485 //------------------------------Identity---------------------------------------
 486 // Fold (x-y)+y  OR  y+(x-y)  into  x
 487 Node* AddLNode::Identity(PhaseGVN* phase) {
 488   if (in(1)->Opcode() == Op_SubL && in(1)->in(2) == in(2)) {
 489     return in(1)->in(1);
 490   } else if (in(2)->Opcode() == Op_SubL && in(2)->in(2) == in(1)) {
 491     return in(2)->in(1);
 492   }
 493   return AddNode::Identity(phase);
 494 }
 495 
 496 
 497 //------------------------------add_ring---------------------------------------
 498 // Supplied function returns the sum of the inputs.  Guaranteed never
 499 // to be passed a TOP or BOTTOM type, these are filtered out by
 500 // pre-check.
 501 const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {
 502   const TypeLong *r0 = t0->is_long(); // Handy access
 503   const TypeLong *r1 = t1->is_long();
 504   jlong lo = java_add(r0->_lo, r1->_lo);
 505   jlong hi = java_add(r0->_hi, r1->_hi);
 506   if( !(r0->is_con() && r1->is_con()) ) {
 507     // Not both constants, compute approximate result
 508     if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
 509       lo =min_jlong; hi = max_jlong; // Underflow on the low side
 510     }
 511     if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
 512       lo = min_jlong; hi = max_jlong; // Overflow on the high side
 513     }
 514     if( lo > hi ) {               // Handle overflow
 515       lo = min_jlong; hi = max_jlong;
 516     }
 517   } else {
 518     // both constants, compute precise result using 'lo' and 'hi'
 519     // Semantics define overflow and underflow for integer addition
 520     // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
 521   }
 522   return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
 523 }
 524 
 525 
 526 //=============================================================================
 527 //------------------------------add_of_identity--------------------------------
 528 // Check for addition of the identity
 529 const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {
 530   // x ADD 0  should return x unless 'x' is a -zero
 531   //
 532   // const Type *zero = add_id();     // The additive identity
 533   // jfloat f1 = t1->getf();
 534   // jfloat f2 = t2->getf();
 535   //
 536   // if( t1->higher_equal( zero ) ) return t2;
 537   // if( t2->higher_equal( zero ) ) return t1;
 538 
 539   return nullptr;
 540 }
 541 
 542 //------------------------------add_ring---------------------------------------
 543 // Supplied function returns the sum of the inputs.
 544 // This also type-checks the inputs for sanity.  Guaranteed never to
 545 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 546 const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {
 547   if (!t0->isa_float_constant() || !t1->isa_float_constant()) {
 548     return bottom_type();
 549   }
 550   return TypeF::make( t0->getf() + t1->getf() );
 551 }
 552 
 553 //------------------------------Ideal------------------------------------------
 554 Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 555   // Floating point additions are not associative because of boundary conditions (infinity)
 556   return commute(phase, this) ? this : nullptr;
 557 }
 558 
 559 
 560 //=============================================================================
 561 //------------------------------add_of_identity--------------------------------
 562 // Check for addition of the identity
 563 const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {
 564   // x ADD 0  should return x unless 'x' is a -zero
 565   //
 566   // const Type *zero = add_id();     // The additive identity
 567   // jfloat f1 = t1->getf();
 568   // jfloat f2 = t2->getf();
 569   //
 570   // if( t1->higher_equal( zero ) ) return t2;
 571   // if( t2->higher_equal( zero ) ) return t1;
 572 
 573   return nullptr;
 574 }
 575 //------------------------------add_ring---------------------------------------
 576 // Supplied function returns the sum of the inputs.
 577 // This also type-checks the inputs for sanity.  Guaranteed never to
 578 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 579 const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {
 580   if (!t0->isa_double_constant() || !t1->isa_double_constant()) {
 581     return bottom_type();
 582   }
 583   return TypeD::make( t0->getd() + t1->getd() );
 584 }
 585 
 586 //------------------------------Ideal------------------------------------------
 587 Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 588   // Floating point additions are not associative because of boundary conditions (infinity)
 589   return commute(phase, this) ? this : nullptr;
 590 }
 591 
 592 
 593 //=============================================================================
 594 //------------------------------Identity---------------------------------------
 595 // If one input is a constant 0, return the other input.
 596 Node* AddPNode::Identity(PhaseGVN* phase) {
 597   return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;
 598 }
 599 
 600 //------------------------------Idealize---------------------------------------
 601 Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 602   // Bail out if dead inputs
 603   if( phase->type( in(Address) ) == Type::TOP ) return nullptr;
 604 
 605   // If the left input is an add of a constant, flatten the expression tree.
 606   const Node *n = in(Address);
 607   if (n->is_AddP() && n->in(Base) == in(Base)) {
 608     const AddPNode *addp = n->as_AddP(); // Left input is an AddP
 609     assert( !addp->in(Address)->is_AddP() ||
 610              addp->in(Address)->as_AddP() != addp,
 611             "dead loop in AddPNode::Ideal" );
 612     // Type of left input's right input
 613     const Type *t = phase->type( addp->in(Offset) );
 614     if( t == Type::TOP ) return nullptr;
 615     const TypeX *t12 = t->is_intptr_t();
 616     if( t12->is_con() ) {       // Left input is an add of a constant?
 617       // If the right input is a constant, combine constants
 618       const Type *temp_t2 = phase->type( in(Offset) );
 619       if( temp_t2 == Type::TOP ) return nullptr;
 620       const TypeX *t2 = temp_t2->is_intptr_t();
 621       Node* address;
 622       Node* offset;
 623       if( t2->is_con() ) {
 624         // The Add of the flattened expression
 625         address = addp->in(Address);
 626         offset  = phase->MakeConX(t2->get_con() + t12->get_con());
 627       } else {
 628         // Else move the constant to the right.  ((A+con)+B) into ((A+B)+con)
 629         address = phase->transform(new AddPNode(in(Base),addp->in(Address),in(Offset)));
 630         offset  = addp->in(Offset);
 631       }
 632       set_req_X(Address, address, phase);
 633       set_req_X(Offset, offset, phase);
 634       return this;
 635     }
 636   }
 637 
 638   // Raw pointers?
 639   if( in(Base)->bottom_type() == Type::TOP ) {
 640     // If this is a null+long form (from unsafe accesses), switch to a rawptr.
 641     if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
 642       Node* offset = in(Offset);
 643       return new CastX2PNode(offset);
 644     }
 645   }
 646 
 647   // If the right is an add of a constant, push the offset down.
 648   // Convert: (ptr + (offset+con)) into (ptr+offset)+con.
 649   // The idea is to merge array_base+scaled_index groups together,
 650   // and only have different constant offsets from the same base.
 651   const Node *add = in(Offset);
 652   if( add->Opcode() == Op_AddX && add->in(1) != add ) {
 653     const Type *t22 = phase->type( add->in(2) );
 654     if( t22->singleton() && (t22 != Type::TOP) ) {  // Right input is an add of a constant?
 655       set_req(Address, phase->transform(new AddPNode(in(Base),in(Address),add->in(1))));
 656       set_req_X(Offset, add->in(2), phase); // puts add on igvn worklist if needed
 657       return this;              // Made progress
 658     }
 659   }
 660 
 661   return nullptr;                  // No progress
 662 }
 663 
 664 //------------------------------bottom_type------------------------------------
 665 // Bottom-type is the pointer-type with unknown offset.
 666 const Type *AddPNode::bottom_type() const {
 667   if (in(Address) == nullptr)  return TypePtr::BOTTOM;
 668   const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
 669   if( !tp ) return Type::TOP;   // TOP input means TOP output
 670   assert( in(Offset)->Opcode() != Op_ConP, "" );
 671   const Type *t = in(Offset)->bottom_type();
 672   if( t == Type::TOP )
 673     return tp->add_offset(Type::OffsetTop);
 674   const TypeX *tx = t->is_intptr_t();
 675   intptr_t txoffset = Type::OffsetBot;
 676   if (tx->is_con()) {   // Left input is an add of a constant?
 677     txoffset = tx->get_con();
 678   }
 679   return tp->add_offset(txoffset);
 680 }
 681 
 682 //------------------------------Value------------------------------------------
 683 const Type* AddPNode::Value(PhaseGVN* phase) const {
 684   // Either input is TOP ==> the result is TOP
 685   const Type *t1 = phase->type( in(Address) );
 686   const Type *t2 = phase->type( in(Offset) );
 687   if( t1 == Type::TOP ) return Type::TOP;
 688   if( t2 == Type::TOP ) return Type::TOP;
 689 
 690   // Left input is a pointer
 691   const TypePtr *p1 = t1->isa_ptr();
 692   // Right input is an int
 693   const TypeX *p2 = t2->is_intptr_t();
 694   // Add 'em
 695   intptr_t p2offset = Type::OffsetBot;
 696   if (p2->is_con()) {   // Left input is an add of a constant?
 697     p2offset = p2->get_con();
 698   }
 699   return p1->add_offset(p2offset);
 700 }
 701 
 702 //------------------------Ideal_base_and_offset--------------------------------
 703 // Split an oop pointer into a base and offset.
 704 // (The offset might be Type::OffsetBot in the case of an array.)
 705 // Return the base, or null if failure.
 706 Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseValues* phase,
 707                                       // second return value:
 708                                       intptr_t& offset) {
 709   if (ptr->is_AddP()) {
 710     Node* base = ptr->in(AddPNode::Base);
 711     Node* addr = ptr->in(AddPNode::Address);
 712     Node* offs = ptr->in(AddPNode::Offset);
 713     if (base == addr || base->is_top()) {
 714       offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
 715       if (offset != Type::OffsetBot) {
 716         return addr;
 717       }
 718     }
 719   }
 720   offset = Type::OffsetBot;
 721   return nullptr;
 722 }
 723 
 724 //------------------------------unpack_offsets----------------------------------
 725 // Collect the AddP offset values into the elements array, giving up
 726 // if there are more than length.
 727 int AddPNode::unpack_offsets(Node* elements[], int length) const {
 728   int count = 0;
 729   Node const* addr = this;
 730   Node* base = addr->in(AddPNode::Base);
 731   while (addr->is_AddP()) {
 732     if (addr->in(AddPNode::Base) != base) {
 733       // give up
 734       return -1;
 735     }
 736     elements[count++] = addr->in(AddPNode::Offset);
 737     if (count == length) {
 738       // give up
 739       return -1;
 740     }
 741     addr = addr->in(AddPNode::Address);
 742   }
 743   if (addr != base) {
 744     return -1;
 745   }
 746   return count;
 747 }
 748 
 749 //------------------------------match_edge-------------------------------------
 750 // Do we Match on this edge index or not?  Do not match base pointer edge
 751 uint AddPNode::match_edge(uint idx) const {
 752   return idx > Base;
 753 }
 754 
 755 //=============================================================================
 756 //------------------------------Identity---------------------------------------
 757 Node* OrINode::Identity(PhaseGVN* phase) {
 758   // x | x => x
 759   if (in(1) == in(2)) {
 760     return in(1);
 761   }
 762 
 763   return AddNode::Identity(phase);
 764 }
 765 
 766 // Find shift value for Integer or Long OR.
 767 static Node* rotate_shift(PhaseGVN* phase, Node* lshift, Node* rshift, int mask) {
 768   // val << norm_con_shift | val >> ({32|64} - norm_con_shift) => rotate_left val, norm_con_shift
 769   const TypeInt* lshift_t = phase->type(lshift)->isa_int();
 770   const TypeInt* rshift_t = phase->type(rshift)->isa_int();
 771   if (lshift_t != nullptr && lshift_t->is_con() &&
 772       rshift_t != nullptr && rshift_t->is_con() &&
 773       ((lshift_t->get_con() & mask) == ((mask + 1) - (rshift_t->get_con() & mask)))) {
 774     return phase->intcon(lshift_t->get_con() & mask);
 775   }
 776   // val << var_shift | val >> ({0|32|64} - var_shift) => rotate_left val, var_shift
 777   if (rshift->Opcode() == Op_SubI && rshift->in(2) == lshift && rshift->in(1)->is_Con()){
 778     const TypeInt* shift_t = phase->type(rshift->in(1))->isa_int();
 779     if (shift_t != nullptr && shift_t->is_con() &&
 780         (shift_t->get_con() == 0 || shift_t->get_con() == (mask + 1))) {
 781       return lshift;
 782     }
 783   }
 784   return nullptr;
 785 }
 786 
 787 Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 788   int lopcode = in(1)->Opcode();
 789   int ropcode = in(2)->Opcode();
 790   if (Matcher::match_rule_supported(Op_RotateLeft) &&
 791       lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) {
 792     Node* lshift = in(1)->in(2);
 793     Node* rshift = in(2)->in(2);
 794     Node* shift = rotate_shift(phase, lshift, rshift, 0x1F);
 795     if (shift != nullptr) {
 796       return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT);
 797     }
 798     return nullptr;
 799   }
 800   if (Matcher::match_rule_supported(Op_RotateRight) &&
 801       lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) {
 802     Node* rshift = in(1)->in(2);
 803     Node* lshift = in(2)->in(2);
 804     Node* shift = rotate_shift(phase, rshift, lshift, 0x1F);
 805     if (shift != nullptr) {
 806       return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
 807     }
 808   }
 809 
 810   // Convert "~a | ~b" into "~(a & b)"
 811   if (AddNode::is_not(phase, in(1), T_INT) && AddNode::is_not(phase, in(2), T_INT)) {
 812     Node* and_a_b = new AndINode(in(1)->in(1), in(2)->in(1));
 813     Node* tn = phase->transform(and_a_b);
 814     return AddNode::make_not(phase, tn, T_INT);
 815   }
 816   return nullptr;
 817 }
 818 
 819 //------------------------------add_ring---------------------------------------
 820 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
 821 // the logical operations the ring's ADD is really a logical OR function.
 822 // This also type-checks the inputs for sanity.  Guaranteed never to
 823 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 824 const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
 825   const TypeInt *r0 = t0->is_int(); // Handy access
 826   const TypeInt *r1 = t1->is_int();
 827 
 828   // If both args are bool, can figure out better types
 829   if ( r0 == TypeInt::BOOL ) {
 830     if ( r1 == TypeInt::ONE) {
 831       return TypeInt::ONE;
 832     } else if ( r1 == TypeInt::BOOL ) {
 833       return TypeInt::BOOL;
 834     }
 835   } else if ( r0 == TypeInt::ONE ) {
 836     if ( r1 == TypeInt::BOOL ) {
 837       return TypeInt::ONE;
 838     }
 839   }
 840 
 841   // If either input is not a constant, just return all integers.
 842   if( !r0->is_con() || !r1->is_con() )
 843     return TypeInt::INT;        // Any integer, but still no symbols.
 844 
 845   // Otherwise just OR them bits.
 846   return TypeInt::make( r0->get_con() | r1->get_con() );
 847 }
 848 
 849 //=============================================================================
 850 //------------------------------Identity---------------------------------------
 851 Node* OrLNode::Identity(PhaseGVN* phase) {
 852   // x | x => x
 853   if (in(1) == in(2)) {
 854     return in(1);
 855   }
 856 
 857   return AddNode::Identity(phase);
 858 }
 859 
 860 Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 861   int lopcode = in(1)->Opcode();
 862   int ropcode = in(2)->Opcode();
 863   if (Matcher::match_rule_supported(Op_RotateLeft) &&
 864       lopcode == Op_LShiftL && ropcode == Op_URShiftL && in(1)->in(1) == in(2)->in(1)) {
 865     Node* lshift = in(1)->in(2);
 866     Node* rshift = in(2)->in(2);
 867     Node* shift = rotate_shift(phase, lshift, rshift, 0x3F);
 868     if (shift != nullptr) {
 869       return new RotateLeftNode(in(1)->in(1), shift, TypeLong::LONG);
 870     }
 871     return nullptr;
 872   }
 873   if (Matcher::match_rule_supported(Op_RotateRight) &&
 874       lopcode == Op_URShiftL && ropcode == Op_LShiftL && in(1)->in(1) == in(2)->in(1)) {
 875     Node* rshift = in(1)->in(2);
 876     Node* lshift = in(2)->in(2);
 877     Node* shift = rotate_shift(phase, rshift, lshift, 0x3F);
 878     if (shift != nullptr) {
 879       return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG);
 880     }
 881   }
 882 
 883   // Convert "~a | ~b" into "~(a & b)"
 884   if (AddNode::is_not(phase, in(1), T_LONG) && AddNode::is_not(phase, in(2), T_LONG)) {
 885     Node* and_a_b = new AndLNode(in(1)->in(1), in(2)->in(1));
 886     Node* tn = phase->transform(and_a_b);
 887     return AddNode::make_not(phase, tn, T_LONG);
 888   }
 889 
 890   return nullptr;
 891 }
 892 
 893 //------------------------------add_ring---------------------------------------
 894 const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
 895   const TypeLong *r0 = t0->is_long(); // Handy access
 896   const TypeLong *r1 = t1->is_long();
 897 
 898   // If either input is not a constant, just return all integers.
 899   if( !r0->is_con() || !r1->is_con() )
 900     return TypeLong::LONG;      // Any integer, but still no symbols.
 901 
 902   // Otherwise just OR them bits.
 903   return TypeLong::make( r0->get_con() | r1->get_con() );
 904 }
 905 
 906 //---------------------------Helper -------------------------------------------
 907 /* Decide if the given node is used only in arithmetic expressions(add or sub).
 908  */
 909 static bool is_used_in_only_arithmetic(Node* n, BasicType bt) {
 910   for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
 911     Node* u = n->fast_out(i);
 912     if (u->Opcode() != Op_Add(bt) && u->Opcode() != Op_Sub(bt)) {
 913       return false;
 914     }
 915   }
 916   return true;
 917 }
 918 
 919 //=============================================================================
 920 //------------------------------Idealize---------------------------------------
 921 Node* XorINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 922   Node* in1 = in(1);
 923   Node* in2 = in(2);
 924 
 925   // Convert ~x into -1-x when ~x is used in an arithmetic expression
 926   // or x itself is an expression.
 927   if (phase->type(in2) == TypeInt::MINUS_1) { // follows LHS^(-1), i.e., ~LHS
 928     if (phase->is_IterGVN()) {
 929       if (is_used_in_only_arithmetic(this, T_INT)
 930           // LHS is arithmetic
 931           || (in1->Opcode() == Op_AddI || in1->Opcode() == Op_SubI)) {
 932         return new SubINode(in2, in1);
 933       }
 934     } else {
 935       // graph could be incomplete in GVN so we postpone to IGVN
 936       phase->record_for_igvn(this);
 937     }
 938   }
 939 
 940   // Propagate xor through constant cmoves. This pattern can occur after expansion of Conv2B nodes.
 941   const TypeInt* in2_type = phase->type(in2)->isa_int();
 942   if (in1->Opcode() == Op_CMoveI && in2_type != nullptr && in2_type->is_con()) {
 943     int in2_val = in2_type->get_con();
 944 
 945     // Get types of both sides of the CMove
 946     const TypeInt* left = phase->type(in1->in(CMoveNode::IfFalse))->isa_int();
 947     const TypeInt* right = phase->type(in1->in(CMoveNode::IfTrue))->isa_int();
 948 
 949     // Ensure that both sides are int constants
 950     if (left != nullptr && right != nullptr && left->is_con() && right->is_con()) {
 951       Node* cond = in1->in(CMoveNode::Condition);
 952 
 953       // Check that the comparison is a bool and that the cmp node type is correct
 954       if (cond->is_Bool()) {
 955         int cmp_op = cond->in(1)->Opcode();
 956 
 957         if (cmp_op == Op_CmpI || cmp_op == Op_CmpP) {
 958           int l_val = left->get_con();
 959           int r_val = right->get_con();
 960 
 961           return new CMoveINode(cond, phase->intcon(l_val ^ in2_val), phase->intcon(r_val ^ in2_val), TypeInt::INT);
 962         }
 963       }
 964     }
 965   }
 966 
 967   return AddNode::Ideal(phase, can_reshape);
 968 }
 969 
 970 const Type* XorINode::Value(PhaseGVN* phase) const {
 971   Node* in1 = in(1);
 972   Node* in2 = in(2);
 973   const Type* t1 = phase->type(in1);
 974   const Type* t2 = phase->type(in2);
 975   if (t1 == Type::TOP || t2 == Type::TOP) {
 976     return Type::TOP;
 977   }
 978   // x ^ x ==> 0
 979   if (in1->eqv_uncast(in2)) {
 980     return add_id();
 981   }
 982   // result of xor can only have bits sets where any of the
 983   // inputs have bits set. lo can always become 0.
 984   const TypeInt* t1i = t1->is_int();
 985   const TypeInt* t2i = t2->is_int();
 986   if ((t1i->_lo >= 0) &&
 987       (t1i->_hi > 0)  &&
 988       (t2i->_lo >= 0) &&
 989       (t2i->_hi > 0)) {
 990     // hi - set all bits below the highest bit. Using round_down to avoid overflow.
 991     const TypeInt* t1x = TypeInt::make(0, round_down_power_of_2(t1i->_hi) + (round_down_power_of_2(t1i->_hi) - 1), t1i->_widen);
 992     const TypeInt* t2x = TypeInt::make(0, round_down_power_of_2(t2i->_hi) + (round_down_power_of_2(t2i->_hi) - 1), t2i->_widen);
 993     return t1x->meet(t2x);
 994   }
 995   return AddNode::Value(phase);
 996 }
 997 
 998 
 999 //------------------------------add_ring---------------------------------------
1000 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
1001 // the logical operations the ring's ADD is really a logical OR function.
1002 // This also type-checks the inputs for sanity.  Guaranteed never to
1003 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
1004 const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
1005   const TypeInt *r0 = t0->is_int(); // Handy access
1006   const TypeInt *r1 = t1->is_int();
1007 
1008   // Complementing a boolean?
1009   if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE
1010                                || r1 == TypeInt::BOOL))
1011     return TypeInt::BOOL;
1012 
1013   if( !r0->is_con() || !r1->is_con() ) // Not constants
1014     return TypeInt::INT;        // Any integer, but still no symbols.
1015 
1016   // Otherwise just XOR them bits.
1017   return TypeInt::make( r0->get_con() ^ r1->get_con() );
1018 }
1019 
1020 //=============================================================================
1021 //------------------------------add_ring---------------------------------------
1022 const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
1023   const TypeLong *r0 = t0->is_long(); // Handy access
1024   const TypeLong *r1 = t1->is_long();
1025 
1026   // If either input is not a constant, just return all integers.
1027   if( !r0->is_con() || !r1->is_con() )
1028     return TypeLong::LONG;      // Any integer, but still no symbols.
1029 
1030   // Otherwise just OR them bits.
1031   return TypeLong::make( r0->get_con() ^ r1->get_con() );
1032 }
1033 
1034 Node* XorLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1035   Node* in1 = in(1);
1036   Node* in2 = in(2);
1037 
1038   // Convert ~x into -1-x when ~x is used in an arithmetic expression
1039   // or x itself is an arithmetic expression.
1040   if (phase->type(in2) == TypeLong::MINUS_1) { // follows LHS^(-1), i.e., ~LHS
1041     if (phase->is_IterGVN()) {
1042       if (is_used_in_only_arithmetic(this, T_LONG)
1043           // LHS is arithmetic
1044           || (in1->Opcode() == Op_AddL || in1->Opcode() == Op_SubL)) {
1045         return new SubLNode(in2, in1);
1046       }
1047     } else {
1048       // graph could be incomplete in GVN so we postpone to IGVN
1049       phase->record_for_igvn(this);
1050     }
1051   }
1052   return AddNode::Ideal(phase, can_reshape);
1053 }
1054 
1055 const Type* XorLNode::Value(PhaseGVN* phase) const {
1056   Node* in1 = in(1);
1057   Node* in2 = in(2);
1058   const Type* t1 = phase->type(in1);
1059   const Type* t2 = phase->type(in2);
1060   if (t1 == Type::TOP || t2 == Type::TOP) {
1061     return Type::TOP;
1062   }
1063   // x ^ x ==> 0
1064   if (in1->eqv_uncast(in2)) {
1065     return add_id();
1066   }
1067   // result of xor can only have bits sets where any of the
1068   // inputs have bits set. lo can always become 0.
1069   const TypeLong* t1l = t1->is_long();
1070   const TypeLong* t2l = t2->is_long();
1071   if ((t1l->_lo >= 0) &&
1072       (t1l->_hi > 0)  &&
1073       (t2l->_lo >= 0) &&
1074       (t2l->_hi > 0)) {
1075     // hi - set all bits below the highest bit. Using round_down to avoid overflow.
1076     const TypeLong* t1x = TypeLong::make(0, round_down_power_of_2(t1l->_hi) + (round_down_power_of_2(t1l->_hi) - 1), t1l->_widen);
1077     const TypeLong* t2x = TypeLong::make(0, round_down_power_of_2(t2l->_hi) + (round_down_power_of_2(t2l->_hi) - 1), t2l->_widen);
1078     return t1x->meet(t2x);
1079   }
1080   return AddNode::Value(phase);
1081 }
1082 
1083 Node* MaxNode::build_min_max_int(Node* a, Node* b, bool is_max) {
1084   if (is_max) {
1085     return new MaxINode(a, b);
1086   } else {
1087     return new MinINode(a, b);
1088   }
1089 }
1090 
1091 Node* MaxNode::build_min_max_long(PhaseGVN* phase, Node* a, Node* b, bool is_max) {
1092   if (is_max) {
1093     return new MaxLNode(phase->C, a, b);
1094   } else {
1095     return new MinLNode(phase->C, a, b);
1096   }
1097 }
1098 
1099 Node* MaxNode::build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn) {
1100   bool is_int = gvn.type(a)->isa_int();
1101   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1102   assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs");
1103   BasicType bt = is_int ? T_INT: T_LONG;
1104   Node* hook = nullptr;
1105   if (gvn.is_IterGVN()) {
1106     // Make sure a and b are not destroyed
1107     hook = new Node(2);
1108     hook->init_req(0, a);
1109     hook->init_req(1, b);
1110   }
1111   Node* res = nullptr;
1112   if (is_int && !is_unsigned) {
1113     res = gvn.transform(build_min_max_int(a, b, is_max));
1114     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");
1115   } else {
1116     Node* cmp = nullptr;
1117     if (is_max) {
1118       cmp = gvn.transform(CmpNode::make(a, b, bt, is_unsigned));
1119     } else {
1120       cmp = gvn.transform(CmpNode::make(b, a, bt, is_unsigned));
1121     }
1122     Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1123     res = gvn.transform(CMoveNode::make(nullptr, bol, a, b, t));
1124   }
1125   if (hook != nullptr) {
1126     hook->destruct(&gvn);
1127   }
1128   return res;
1129 }
1130 
1131 Node* MaxNode::build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn) {
1132   bool is_int = gvn.type(a)->isa_int();
1133   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1134   assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs");
1135   BasicType bt = is_int ? T_INT: T_LONG;
1136   Node* zero = gvn.integercon(0, bt);
1137   Node* hook = nullptr;
1138   if (gvn.is_IterGVN()) {
1139     // Make sure a and b are not destroyed
1140     hook = new Node(2);
1141     hook->init_req(0, a);
1142     hook->init_req(1, b);
1143   }
1144   Node* cmp = nullptr;
1145   if (is_max) {
1146     cmp = gvn.transform(CmpNode::make(a, b, bt, false));
1147   } else {
1148     cmp = gvn.transform(CmpNode::make(b, a, bt, false));
1149   }
1150   Node* sub = gvn.transform(SubNode::make(a, b, bt));
1151   Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1152   Node* res = gvn.transform(CMoveNode::make(nullptr, bol, sub, zero, t));
1153   if (hook != nullptr) {
1154     hook->destruct(&gvn);
1155   }
1156   return res;
1157 }
1158 
1159 // Check if addition of an integer with type 't' and a constant 'c' can overflow.
1160 static bool can_overflow(const TypeInt* t, jint c) {
1161   jint t_lo = t->_lo;
1162   jint t_hi = t->_hi;
1163   return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1164           (c > 0 && (java_add(t_hi, c) < t_hi)));
1165 }
1166 
1167 // Let <x, x_off> = x_operands and <y, y_off> = y_operands.
1168 // If x == y and neither add(x, x_off) nor add(y, y_off) overflow, return
1169 // add(x, op(x_off, y_off)). Otherwise, return nullptr.
1170 Node* MaxNode::extract_add(PhaseGVN* phase, ConstAddOperands x_operands, ConstAddOperands y_operands) {
1171   Node* x = x_operands.first;
1172   Node* y = y_operands.first;
1173   int opcode = Opcode();
1174   assert(opcode == Op_MaxI || opcode == Op_MinI, "Unexpected opcode");
1175   const TypeInt* tx = phase->type(x)->isa_int();
1176   jint x_off = x_operands.second;
1177   jint y_off = y_operands.second;
1178   if (x == y && tx != nullptr &&
1179       !can_overflow(tx, x_off) &&
1180       !can_overflow(tx, y_off)) {
1181     jint c = opcode == Op_MinI ? MIN2(x_off, y_off) : MAX2(x_off, y_off);
1182     return new AddINode(x, phase->intcon(c));
1183   }
1184   return nullptr;
1185 }
1186 
1187 // Try to cast n as an integer addition with a constant. Return:
1188 //   <x, C>,       if n == add(x, C), where 'C' is a non-TOP constant;
1189 //   <nullptr, 0>, if n == add(x, C), where 'C' is a TOP constant; or
1190 //   <n, 0>,       otherwise.
1191 static ConstAddOperands as_add_with_constant(Node* n) {
1192   if (n->Opcode() != Op_AddI) {
1193     return ConstAddOperands(n, 0);
1194   }
1195   Node* x = n->in(1);
1196   Node* c = n->in(2);
1197   if (!c->is_Con()) {
1198     return ConstAddOperands(n, 0);
1199   }
1200   const Type* c_type = c->bottom_type();
1201   if (c_type == Type::TOP) {
1202     return ConstAddOperands(nullptr, 0);
1203   }
1204   return ConstAddOperands(x, c_type->is_int()->get_con());
1205 }
1206 
1207 Node* MaxNode::IdealI(PhaseGVN* phase, bool can_reshape) {
1208   int opcode = Opcode();
1209   assert(opcode == Op_MinI || opcode == Op_MaxI, "Unexpected opcode");
1210   // Try to transform the following pattern, in any of its four possible
1211   // permutations induced by op's commutativity:
1212   //     op(op(add(inner, inner_off), inner_other), add(outer, outer_off))
1213   // into
1214   //     op(add(inner, op(inner_off, outer_off)), inner_other),
1215   // where:
1216   //     op is either MinI or MaxI, and
1217   //     inner == outer, and
1218   //     the additions cannot overflow.
1219   for (uint inner_op_index = 1; inner_op_index <= 2; inner_op_index++) {
1220     if (in(inner_op_index)->Opcode() != opcode) {
1221       continue;
1222     }
1223     Node* outer_add = in(inner_op_index == 1 ? 2 : 1);
1224     ConstAddOperands outer_add_operands = as_add_with_constant(outer_add);
1225     if (outer_add_operands.first == nullptr) {
1226       return nullptr; // outer_add has a TOP input, no need to continue.
1227     }
1228     // One operand is a MinI/MaxI and the other is an integer addition with
1229     // constant. Test the operands of the inner MinI/MaxI.
1230     for (uint inner_add_index = 1; inner_add_index <= 2; inner_add_index++) {
1231       Node* inner_op = in(inner_op_index);
1232       Node* inner_add = inner_op->in(inner_add_index);
1233       ConstAddOperands inner_add_operands = as_add_with_constant(inner_add);
1234       if (inner_add_operands.first == nullptr) {
1235         return nullptr; // inner_add has a TOP input, no need to continue.
1236       }
1237       // Try to extract the inner add.
1238       Node* add_extracted = extract_add(phase, inner_add_operands, outer_add_operands);
1239       if (add_extracted == nullptr) {
1240         continue;
1241       }
1242       Node* add_transformed = phase->transform(add_extracted);
1243       Node* inner_other = inner_op->in(inner_add_index == 1 ? 2 : 1);
1244       return build_min_max_int(add_transformed, inner_other, opcode == Op_MaxI);
1245     }
1246   }
1247   // Try to transform
1248   //     op(add(x, x_off), add(y, y_off))
1249   // into
1250   //     add(x, op(x_off, y_off)),
1251   // where:
1252   //     op is either MinI or MaxI, and
1253   //     inner == outer, and
1254   //     the additions cannot overflow.
1255   ConstAddOperands xC = as_add_with_constant(in(1));
1256   ConstAddOperands yC = as_add_with_constant(in(2));
1257   if (xC.first == nullptr || yC.first == nullptr) return nullptr;
1258   return extract_add(phase, xC, yC);
1259 }
1260 
1261 // Ideal transformations for MaxINode
1262 Node* MaxINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1263   return IdealI(phase, can_reshape);
1264 }
1265 
1266 //=============================================================================
1267 //------------------------------add_ring---------------------------------------
1268 // Supplied function returns the sum of the inputs.
1269 const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
1270   const TypeInt *r0 = t0->is_int(); // Handy access
1271   const TypeInt *r1 = t1->is_int();
1272 
1273   // Otherwise just MAX them bits.
1274   return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1275 }
1276 
1277 //=============================================================================
1278 //------------------------------Idealize---------------------------------------
1279 // MINs show up in range-check loop limit calculations.  Look for
1280 // "MIN2(x+c0,MIN2(y,x+c1))".  Pick the smaller constant: "MIN2(x+c0,y)"
1281 Node* MinINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1282   return IdealI(phase, can_reshape);
1283 }
1284 
1285 //------------------------------add_ring---------------------------------------
1286 // Supplied function returns the sum of the inputs.
1287 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
1288   const TypeInt *r0 = t0->is_int(); // Handy access
1289   const TypeInt *r1 = t1->is_int();
1290 
1291   // Otherwise just MIN them bits.
1292   return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1293 }
1294 
1295 // Collapse the "addition with overflow-protection" pattern, and the symmetrical
1296 // "subtraction with underflow-protection" pattern. These are created during the
1297 // unrolling, when we have to adjust the limit by subtracting the stride, but want
1298 // to protect against underflow: MaxL(SubL(limit, stride), min_jint).
1299 // If we have more than one of those in a sequence:
1300 //
1301 //   x  con2
1302 //   |  |
1303 //   AddL  clamp2
1304 //     |    |
1305 //    Max/MinL con1
1306 //          |  |
1307 //          AddL  clamp1
1308 //            |    |
1309 //           Max/MinL (n)
1310 //
1311 // We want to collapse it to:
1312 //
1313 //   x  con1  con2
1314 //   |    |    |
1315 //   |   AddLNode (new_con)
1316 //   |    |
1317 //  AddLNode  clamp1
1318 //        |    |
1319 //       Max/MinL (n)
1320 //
1321 // Note: we assume that SubL was already replaced by an AddL, and that the stride
1322 // has its sign flipped: SubL(limit, stride) -> AddL(limit, -stride).
1323 static Node* fold_subI_no_underflow_pattern(Node* n, PhaseGVN* phase) {
1324   assert(n->Opcode() == Op_MaxL || n->Opcode() == Op_MinL, "sanity");
1325   // Check that the two clamps have the correct values.
1326   jlong clamp = (n->Opcode() == Op_MaxL) ? min_jint : max_jint;
1327   auto is_clamp = [&](Node* c) {
1328     const TypeLong* t = phase->type(c)->isa_long();
1329     return t != nullptr && t->is_con() &&
1330            t->get_con() == clamp;
1331   };
1332   // Check that the constants are negative if MaxL, and positive if MinL.
1333   auto is_sub_con = [&](Node* c) {
1334     const TypeLong* t = phase->type(c)->isa_long();
1335     return t != nullptr && t->is_con() &&
1336            t->get_con() < max_jint && t->get_con() > min_jint &&
1337            (t->get_con() < 0) == (n->Opcode() == Op_MaxL);
1338   };
1339   // Verify the graph level by level:
1340   Node* add1   = n->in(1);
1341   Node* clamp1 = n->in(2);
1342   if (add1->Opcode() == Op_AddL && is_clamp(clamp1)) {
1343     Node* max2 = add1->in(1);
1344     Node* con1 = add1->in(2);
1345     if (max2->Opcode() == n->Opcode() && is_sub_con(con1)) {
1346       Node* add2   = max2->in(1);
1347       Node* clamp2 = max2->in(2);
1348       if (add2->Opcode() == Op_AddL && is_clamp(clamp2)) {
1349         Node* x    = add2->in(1);
1350         Node* con2 = add2->in(2);
1351         if (is_sub_con(con2)) {
1352           Node* new_con = phase->transform(new AddLNode(con1, con2));
1353           Node* new_sub = phase->transform(new AddLNode(x, new_con));
1354           n->set_req_X(1, new_sub, phase);
1355           return n;
1356         }
1357       }
1358     }
1359   }
1360   return nullptr;
1361 }
1362 
1363 const Type* MaxLNode::add_ring(const Type* t0, const Type* t1) const {
1364   const TypeLong* r0 = t0->is_long();
1365   const TypeLong* r1 = t1->is_long();
1366 
1367   return TypeLong::make(MAX2(r0->_lo, r1->_lo), MAX2(r0->_hi, r1->_hi), MAX2(r0->_widen, r1->_widen));
1368 }
1369 
1370 Node* MaxLNode::Identity(PhaseGVN* phase) {
1371   const TypeLong* t1 = phase->type(in(1))->is_long();
1372   const TypeLong* t2 = phase->type(in(2))->is_long();
1373 
1374   // Can we determine maximum statically?
1375   if (t1->_lo >= t2->_hi) {
1376     return in(1);
1377   } else if (t2->_lo >= t1->_hi) {
1378     return in(2);
1379   }
1380 
1381   return MaxNode::Identity(phase);
1382 }
1383 
1384 Node* MaxLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1385   Node* n = AddNode::Ideal(phase, can_reshape);
1386   if (n != nullptr) {
1387     return n;
1388   }
1389   if (can_reshape) {
1390     return fold_subI_no_underflow_pattern(this, phase);
1391   }
1392   return nullptr;
1393 }
1394 
1395 const Type* MinLNode::add_ring(const Type* t0, const Type* t1) const {
1396   const TypeLong* r0 = t0->is_long();
1397   const TypeLong* r1 = t1->is_long();
1398 
1399   return TypeLong::make(MIN2(r0->_lo, r1->_lo), MIN2(r0->_hi, r1->_hi), MIN2(r0->_widen, r1->_widen));
1400 }
1401 
1402 Node* MinLNode::Identity(PhaseGVN* phase) {
1403   const TypeLong* t1 = phase->type(in(1))->is_long();
1404   const TypeLong* t2 = phase->type(in(2))->is_long();
1405 
1406   // Can we determine minimum statically?
1407   if (t1->_lo >= t2->_hi) {
1408     return in(2);
1409   } else if (t2->_lo >= t1->_hi) {
1410     return in(1);
1411   }
1412 
1413   return MaxNode::Identity(phase);
1414 }
1415 
1416 Node* MinLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1417   Node* n = AddNode::Ideal(phase, can_reshape);
1418   if (n != nullptr) {
1419     return n;
1420   }
1421   if (can_reshape) {
1422     return fold_subI_no_underflow_pattern(this, phase);
1423   }
1424   return nullptr;
1425 }
1426 
1427 Node* MaxNode::Identity(PhaseGVN* phase) {
1428   if (in(1) == in(2)) {
1429       return in(1);
1430   }
1431 
1432   return AddNode::Identity(phase);
1433 }
1434 
1435 //------------------------------add_ring---------------------------------------
1436 const Type* MinFNode::add_ring(const Type* t0, const Type* t1 ) const {
1437   const TypeF* r0 = t0->isa_float_constant();
1438   const TypeF* r1 = t1->isa_float_constant();
1439   if (r0 == nullptr || r1 == nullptr) {
1440     return bottom_type();
1441   }
1442 
1443   if (r0->is_nan()) {
1444     return r0;
1445   }
1446   if (r1->is_nan()) {
1447     return r1;
1448   }
1449 
1450   float f0 = r0->getf();
1451   float f1 = r1->getf();
1452   if (f0 != 0.0f || f1 != 0.0f) {
1453     return f0 < f1 ? r0 : r1;
1454   }
1455 
1456   // handle min of 0.0, -0.0 case.
1457   return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1;
1458 }
1459 
1460 //------------------------------add_ring---------------------------------------
1461 const Type* MinDNode::add_ring(const Type* t0, const Type* t1) const {
1462   const TypeD* r0 = t0->isa_double_constant();
1463   const TypeD* r1 = t1->isa_double_constant();
1464   if (r0 == nullptr || r1 == nullptr) {
1465     return bottom_type();
1466   }
1467 
1468   if (r0->is_nan()) {
1469     return r0;
1470   }
1471   if (r1->is_nan()) {
1472     return r1;
1473   }
1474 
1475   double d0 = r0->getd();
1476   double d1 = r1->getd();
1477   if (d0 != 0.0 || d1 != 0.0) {
1478     return d0 < d1 ? r0 : r1;
1479   }
1480 
1481   // handle min of 0.0, -0.0 case.
1482   return (jlong_cast(d0) < jlong_cast(d1)) ? r0 : r1;
1483 }
1484 
1485 //------------------------------add_ring---------------------------------------
1486 const Type* MaxFNode::add_ring(const Type* t0, const Type* t1) const {
1487   const TypeF* r0 = t0->isa_float_constant();
1488   const TypeF* r1 = t1->isa_float_constant();
1489   if (r0 == nullptr || r1 == nullptr) {
1490     return bottom_type();
1491   }
1492 
1493   if (r0->is_nan()) {
1494     return r0;
1495   }
1496   if (r1->is_nan()) {
1497     return r1;
1498   }
1499 
1500   float f0 = r0->getf();
1501   float f1 = r1->getf();
1502   if (f0 != 0.0f || f1 != 0.0f) {
1503     return f0 > f1 ? r0 : r1;
1504   }
1505 
1506   // handle max of 0.0,-0.0 case.
1507   return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1;
1508 }
1509 
1510 //------------------------------add_ring---------------------------------------
1511 const Type* MaxDNode::add_ring(const Type* t0, const Type* t1) const {
1512   const TypeD* r0 = t0->isa_double_constant();
1513   const TypeD* r1 = t1->isa_double_constant();
1514   if (r0 == nullptr || r1 == nullptr) {
1515     return bottom_type();
1516   }
1517 
1518   if (r0->is_nan()) {
1519     return r0;
1520   }
1521   if (r1->is_nan()) {
1522     return r1;
1523   }
1524 
1525   double d0 = r0->getd();
1526   double d1 = r1->getd();
1527   if (d0 != 0.0 || d1 != 0.0) {
1528     return d0 > d1 ? r0 : r1;
1529   }
1530 
1531   // handle max of 0.0, -0.0 case.
1532   return (jlong_cast(d0) > jlong_cast(d1)) ? r0 : r1;
1533 }