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   if (tp->isa_aryptr()) {
 680     // In the case of a flat inline type array, each field has its
 681     // own slice so we need to extract the field being accessed from
 682     // the address computation
 683     return tp->is_aryptr()->add_field_offset_and_offset(txoffset);
 684   }
 685   return tp->add_offset(txoffset);
 686 }
 687 
 688 //------------------------------Value------------------------------------------
 689 const Type* AddPNode::Value(PhaseGVN* phase) const {
 690   // Either input is TOP ==> the result is TOP
 691   const Type *t1 = phase->type( in(Address) );
 692   const Type *t2 = phase->type( in(Offset) );
 693   if( t1 == Type::TOP ) return Type::TOP;
 694   if( t2 == Type::TOP ) return Type::TOP;
 695 
 696   // Left input is a pointer
 697   const TypePtr *p1 = t1->isa_ptr();
 698   // Right input is an int
 699   const TypeX *p2 = t2->is_intptr_t();
 700   // Add 'em
 701   intptr_t p2offset = Type::OffsetBot;
 702   if (p2->is_con()) {   // Left input is an add of a constant?
 703     p2offset = p2->get_con();
 704   }
 705   if (p1->isa_aryptr()) {
 706     // In the case of a flat inline type array, each field has its
 707     // own slice so we need to extract the field being accessed from
 708     // the address computation
 709     return p1->is_aryptr()->add_field_offset_and_offset(p2offset);
 710   }
 711   return p1->add_offset(p2offset);
 712 }
 713 
 714 //------------------------Ideal_base_and_offset--------------------------------
 715 // Split an oop pointer into a base and offset.
 716 // (The offset might be Type::OffsetBot in the case of an array.)
 717 // Return the base, or null if failure.
 718 Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseValues* phase,
 719                                       // second return value:
 720                                       intptr_t& offset) {
 721   if (ptr->is_AddP()) {
 722     Node* base = ptr->in(AddPNode::Base);
 723     Node* addr = ptr->in(AddPNode::Address);
 724     Node* offs = ptr->in(AddPNode::Offset);
 725     if (base == addr || base->is_top()) {
 726       offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
 727       if (offset != Type::OffsetBot) {
 728         return addr;
 729       }
 730     }
 731   }
 732   offset = Type::OffsetBot;
 733   return nullptr;
 734 }
 735 
 736 //------------------------------unpack_offsets----------------------------------
 737 // Collect the AddP offset values into the elements array, giving up
 738 // if there are more than length.
 739 int AddPNode::unpack_offsets(Node* elements[], int length) {
 740   int count = 0;
 741   Node* addr = this;
 742   Node* base = addr->in(AddPNode::Base);
 743   while (addr->is_AddP()) {
 744     if (addr->in(AddPNode::Base) != base) {
 745       // give up
 746       return -1;
 747     }
 748     elements[count++] = addr->in(AddPNode::Offset);
 749     if (count == length) {
 750       // give up
 751       return -1;
 752     }
 753     addr = addr->in(AddPNode::Address);
 754   }
 755   if (addr != base) {
 756     return -1;
 757   }
 758   return count;
 759 }
 760 
 761 //------------------------------match_edge-------------------------------------
 762 // Do we Match on this edge index or not?  Do not match base pointer edge
 763 uint AddPNode::match_edge(uint idx) const {
 764   return idx > Base;
 765 }
 766 
 767 //=============================================================================
 768 //------------------------------Identity---------------------------------------
 769 Node* OrINode::Identity(PhaseGVN* phase) {
 770   // x | x => x
 771   if (in(1) == in(2)) {
 772     return in(1);
 773   }
 774 
 775   return AddNode::Identity(phase);
 776 }
 777 
 778 // Find shift value for Integer or Long OR.
 779 static Node* rotate_shift(PhaseGVN* phase, Node* lshift, Node* rshift, int mask) {
 780   // val << norm_con_shift | val >> ({32|64} - norm_con_shift) => rotate_left val, norm_con_shift
 781   const TypeInt* lshift_t = phase->type(lshift)->isa_int();
 782   const TypeInt* rshift_t = phase->type(rshift)->isa_int();
 783   if (lshift_t != nullptr && lshift_t->is_con() &&
 784       rshift_t != nullptr && rshift_t->is_con() &&
 785       ((lshift_t->get_con() & mask) == ((mask + 1) - (rshift_t->get_con() & mask)))) {
 786     return phase->intcon(lshift_t->get_con() & mask);
 787   }
 788   // val << var_shift | val >> ({0|32|64} - var_shift) => rotate_left val, var_shift
 789   if (rshift->Opcode() == Op_SubI && rshift->in(2) == lshift && rshift->in(1)->is_Con()){
 790     const TypeInt* shift_t = phase->type(rshift->in(1))->isa_int();
 791     if (shift_t != nullptr && shift_t->is_con() &&
 792         (shift_t->get_con() == 0 || shift_t->get_con() == (mask + 1))) {
 793       return lshift;
 794     }
 795   }
 796   return nullptr;
 797 }
 798 
 799 Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 800   int lopcode = in(1)->Opcode();
 801   int ropcode = in(2)->Opcode();
 802   if (Matcher::match_rule_supported(Op_RotateLeft) &&
 803       lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) {
 804     Node* lshift = in(1)->in(2);
 805     Node* rshift = in(2)->in(2);
 806     Node* shift = rotate_shift(phase, lshift, rshift, 0x1F);
 807     if (shift != nullptr) {
 808       return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT);
 809     }
 810     return nullptr;
 811   }
 812   if (Matcher::match_rule_supported(Op_RotateRight) &&
 813       lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) {
 814     Node* rshift = in(1)->in(2);
 815     Node* lshift = in(2)->in(2);
 816     Node* shift = rotate_shift(phase, rshift, lshift, 0x1F);
 817     if (shift != nullptr) {
 818       return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
 819     }
 820   }
 821 
 822   // Convert "~a | ~b" into "~(a & b)"
 823   if (AddNode::is_not(phase, in(1), T_INT) && AddNode::is_not(phase, in(2), T_INT)) {
 824     Node* and_a_b = new AndINode(in(1)->in(1), in(2)->in(1));
 825     Node* tn = phase->transform(and_a_b);
 826     return AddNode::make_not(phase, tn, T_INT);
 827   }
 828   return nullptr;
 829 }
 830 
 831 //------------------------------add_ring---------------------------------------
 832 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
 833 // the logical operations the ring's ADD is really a logical OR function.
 834 // This also type-checks the inputs for sanity.  Guaranteed never to
 835 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 836 const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
 837   const TypeInt *r0 = t0->is_int(); // Handy access
 838   const TypeInt *r1 = t1->is_int();
 839 
 840   // If both args are bool, can figure out better types
 841   if ( r0 == TypeInt::BOOL ) {
 842     if ( r1 == TypeInt::ONE) {
 843       return TypeInt::ONE;
 844     } else if ( r1 == TypeInt::BOOL ) {
 845       return TypeInt::BOOL;
 846     }
 847   } else if ( r0 == TypeInt::ONE ) {
 848     if ( r1 == TypeInt::BOOL ) {
 849       return TypeInt::ONE;
 850     }
 851   }
 852 
 853   // If either input is not a constant, just return all integers.
 854   if( !r0->is_con() || !r1->is_con() )
 855     return TypeInt::INT;        // Any integer, but still no symbols.
 856 
 857   // Otherwise just OR them bits.
 858   return TypeInt::make( r0->get_con() | r1->get_con() );
 859 }
 860 
 861 //=============================================================================
 862 //------------------------------Identity---------------------------------------
 863 Node* OrLNode::Identity(PhaseGVN* phase) {
 864   // x | x => x
 865   if (in(1) == in(2)) {
 866     return in(1);
 867   }
 868 
 869   return AddNode::Identity(phase);
 870 }
 871 
 872 Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 873   int lopcode = in(1)->Opcode();
 874   int ropcode = in(2)->Opcode();
 875   if (Matcher::match_rule_supported(Op_RotateLeft) &&
 876       lopcode == Op_LShiftL && ropcode == Op_URShiftL && in(1)->in(1) == in(2)->in(1)) {
 877     Node* lshift = in(1)->in(2);
 878     Node* rshift = in(2)->in(2);
 879     Node* shift = rotate_shift(phase, lshift, rshift, 0x3F);
 880     if (shift != nullptr) {
 881       return new RotateLeftNode(in(1)->in(1), shift, TypeLong::LONG);
 882     }
 883     return nullptr;
 884   }
 885   if (Matcher::match_rule_supported(Op_RotateRight) &&
 886       lopcode == Op_URShiftL && ropcode == Op_LShiftL && in(1)->in(1) == in(2)->in(1)) {
 887     Node* rshift = in(1)->in(2);
 888     Node* lshift = in(2)->in(2);
 889     Node* shift = rotate_shift(phase, rshift, lshift, 0x3F);
 890     if (shift != nullptr) {
 891       return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG);
 892     }
 893   }
 894 
 895   // Convert "~a | ~b" into "~(a & b)"
 896   if (AddNode::is_not(phase, in(1), T_LONG) && AddNode::is_not(phase, in(2), T_LONG)) {
 897     Node* and_a_b = new AndLNode(in(1)->in(1), in(2)->in(1));
 898     Node* tn = phase->transform(and_a_b);
 899     return AddNode::make_not(phase, tn, T_LONG);
 900   }
 901 
 902   return nullptr;
 903 }
 904 
 905 //------------------------------add_ring---------------------------------------
 906 const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
 907   const TypeLong *r0 = t0->is_long(); // Handy access
 908   const TypeLong *r1 = t1->is_long();
 909 
 910   // If either input is not a constant, just return all integers.
 911   if( !r0->is_con() || !r1->is_con() )
 912     return TypeLong::LONG;      // Any integer, but still no symbols.
 913 
 914   // Otherwise just OR them bits.
 915   return TypeLong::make( r0->get_con() | r1->get_con() );
 916 }
 917 
 918 //---------------------------Helper -------------------------------------------
 919 /* Decide if the given node is used only in arithmetic expressions(add or sub).
 920  */
 921 static bool is_used_in_only_arithmetic(Node* n, BasicType bt) {
 922   for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
 923     Node* u = n->fast_out(i);
 924     if (u->Opcode() != Op_Add(bt) && u->Opcode() != Op_Sub(bt)) {
 925       return false;
 926     }
 927   }
 928   return true;
 929 }
 930 
 931 //=============================================================================
 932 //------------------------------Idealize---------------------------------------
 933 Node* XorINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 934   Node* in1 = in(1);
 935   Node* in2 = in(2);
 936 
 937   // Convert ~x into -1-x when ~x is used in an arithmetic expression
 938   // or x itself is an expression.
 939   if (phase->type(in2) == TypeInt::MINUS_1) { // follows LHS^(-1), i.e., ~LHS
 940     if (phase->is_IterGVN()) {
 941       if (is_used_in_only_arithmetic(this, T_INT)
 942           // LHS is arithmetic
 943           || (in1->Opcode() == Op_AddI || in1->Opcode() == Op_SubI)) {
 944         return new SubINode(in2, in1);
 945       }
 946     } else {
 947       // graph could be incomplete in GVN so we postpone to IGVN
 948       phase->record_for_igvn(this);
 949     }
 950   }
 951 
 952   // Propagate xor through constant cmoves. This pattern can occur after expansion of Conv2B nodes.
 953   const TypeInt* in2_type = phase->type(in2)->isa_int();
 954   if (in1->Opcode() == Op_CMoveI && in2_type != nullptr && in2_type->is_con()) {
 955     int in2_val = in2_type->get_con();
 956 
 957     // Get types of both sides of the CMove
 958     const TypeInt* left = phase->type(in1->in(CMoveNode::IfFalse))->isa_int();
 959     const TypeInt* right = phase->type(in1->in(CMoveNode::IfTrue))->isa_int();
 960 
 961     // Ensure that both sides are int constants
 962     if (left != nullptr && right != nullptr && left->is_con() && right->is_con()) {
 963       Node* cond = in1->in(CMoveNode::Condition);
 964 
 965       // Check that the comparison is a bool and that the cmp node type is correct
 966       if (cond->is_Bool()) {
 967         int cmp_op = cond->in(1)->Opcode();
 968 
 969         if (cmp_op == Op_CmpI || cmp_op == Op_CmpP) {
 970           int l_val = left->get_con();
 971           int r_val = right->get_con();
 972 
 973           return new CMoveINode(cond, phase->intcon(l_val ^ in2_val), phase->intcon(r_val ^ in2_val), TypeInt::INT);
 974         }
 975       }
 976     }
 977   }
 978 
 979   return AddNode::Ideal(phase, can_reshape);
 980 }
 981 
 982 const Type* XorINode::Value(PhaseGVN* phase) const {
 983   Node* in1 = in(1);
 984   Node* in2 = in(2);
 985   const Type* t1 = phase->type(in1);
 986   const Type* t2 = phase->type(in2);
 987   if (t1 == Type::TOP || t2 == Type::TOP) {
 988     return Type::TOP;
 989   }
 990   // x ^ x ==> 0
 991   if (in1->eqv_uncast(in2)) {
 992     return add_id();
 993   }
 994   // result of xor can only have bits sets where any of the
 995   // inputs have bits set. lo can always become 0.
 996   const TypeInt* t1i = t1->is_int();
 997   const TypeInt* t2i = t2->is_int();
 998   if ((t1i->_lo >= 0) &&
 999       (t1i->_hi > 0)  &&
1000       (t2i->_lo >= 0) &&
1001       (t2i->_hi > 0)) {
1002     // hi - set all bits below the highest bit. Using round_down to avoid overflow.
1003     const TypeInt* t1x = TypeInt::make(0, round_down_power_of_2(t1i->_hi) + (round_down_power_of_2(t1i->_hi) - 1), t1i->_widen);
1004     const TypeInt* t2x = TypeInt::make(0, round_down_power_of_2(t2i->_hi) + (round_down_power_of_2(t2i->_hi) - 1), t2i->_widen);
1005     return t1x->meet(t2x);
1006   }
1007   return AddNode::Value(phase);
1008 }
1009 
1010 
1011 //------------------------------add_ring---------------------------------------
1012 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
1013 // the logical operations the ring's ADD is really a logical OR function.
1014 // This also type-checks the inputs for sanity.  Guaranteed never to
1015 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
1016 const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
1017   const TypeInt *r0 = t0->is_int(); // Handy access
1018   const TypeInt *r1 = t1->is_int();
1019 
1020   // Complementing a boolean?
1021   if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE
1022                                || r1 == TypeInt::BOOL))
1023     return TypeInt::BOOL;
1024 
1025   if( !r0->is_con() || !r1->is_con() ) // Not constants
1026     return TypeInt::INT;        // Any integer, but still no symbols.
1027 
1028   // Otherwise just XOR them bits.
1029   return TypeInt::make( r0->get_con() ^ r1->get_con() );
1030 }
1031 
1032 //=============================================================================
1033 //------------------------------add_ring---------------------------------------
1034 const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
1035   const TypeLong *r0 = t0->is_long(); // Handy access
1036   const TypeLong *r1 = t1->is_long();
1037 
1038   // If either input is not a constant, just return all integers.
1039   if( !r0->is_con() || !r1->is_con() )
1040     return TypeLong::LONG;      // Any integer, but still no symbols.
1041 
1042   // Otherwise just OR them bits.
1043   return TypeLong::make( r0->get_con() ^ r1->get_con() );
1044 }
1045 
1046 Node* XorLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1047   Node* in1 = in(1);
1048   Node* in2 = in(2);
1049 
1050   // Convert ~x into -1-x when ~x is used in an arithmetic expression
1051   // or x itself is an arithmetic expression.
1052   if (phase->type(in2) == TypeLong::MINUS_1) { // follows LHS^(-1), i.e., ~LHS
1053     if (phase->is_IterGVN()) {
1054       if (is_used_in_only_arithmetic(this, T_LONG)
1055           // LHS is arithmetic
1056           || (in1->Opcode() == Op_AddL || in1->Opcode() == Op_SubL)) {
1057         return new SubLNode(in2, in1);
1058       }
1059     } else {
1060       // graph could be incomplete in GVN so we postpone to IGVN
1061       phase->record_for_igvn(this);
1062     }
1063   }
1064   return AddNode::Ideal(phase, can_reshape);
1065 }
1066 
1067 const Type* XorLNode::Value(PhaseGVN* phase) const {
1068   Node* in1 = in(1);
1069   Node* in2 = in(2);
1070   const Type* t1 = phase->type(in1);
1071   const Type* t2 = phase->type(in2);
1072   if (t1 == Type::TOP || t2 == Type::TOP) {
1073     return Type::TOP;
1074   }
1075   // x ^ x ==> 0
1076   if (in1->eqv_uncast(in2)) {
1077     return add_id();
1078   }
1079   // result of xor can only have bits sets where any of the
1080   // inputs have bits set. lo can always become 0.
1081   const TypeLong* t1l = t1->is_long();
1082   const TypeLong* t2l = t2->is_long();
1083   if ((t1l->_lo >= 0) &&
1084       (t1l->_hi > 0)  &&
1085       (t2l->_lo >= 0) &&
1086       (t2l->_hi > 0)) {
1087     // hi - set all bits below the highest bit. Using round_down to avoid overflow.
1088     const TypeLong* t1x = TypeLong::make(0, round_down_power_of_2(t1l->_hi) + (round_down_power_of_2(t1l->_hi) - 1), t1l->_widen);
1089     const TypeLong* t2x = TypeLong::make(0, round_down_power_of_2(t2l->_hi) + (round_down_power_of_2(t2l->_hi) - 1), t2l->_widen);
1090     return t1x->meet(t2x);
1091   }
1092   return AddNode::Value(phase);
1093 }
1094 
1095 static Node* build_min_max_int(Node* a, Node* b, bool is_max) {
1096   if (is_max) {
1097     return new MaxINode(a, b);
1098   } else {
1099     return new MinINode(a, b);
1100   }
1101 }
1102 
1103 Node* MaxNode::build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn) {
1104   bool is_int = gvn.type(a)->isa_int();
1105   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1106   assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs");
1107   BasicType bt = is_int ? T_INT: T_LONG;
1108   Node* hook = nullptr;
1109   if (gvn.is_IterGVN()) {
1110     // Make sure a and b are not destroyed
1111     hook = new Node(2);
1112     hook->init_req(0, a);
1113     hook->init_req(1, b);
1114   }
1115   Node* res = nullptr;
1116   if (is_int && !is_unsigned) {
1117     res = gvn.transform(build_min_max_int(a, b, is_max));
1118     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");
1119   } else {
1120     Node* cmp = nullptr;
1121     if (is_max) {
1122       cmp = gvn.transform(CmpNode::make(a, b, bt, is_unsigned));
1123     } else {
1124       cmp = gvn.transform(CmpNode::make(b, a, bt, is_unsigned));
1125     }
1126     Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1127     res = gvn.transform(CMoveNode::make(nullptr, bol, a, b, t));
1128   }
1129   if (hook != nullptr) {
1130     hook->destruct(&gvn);
1131   }
1132   return res;
1133 }
1134 
1135 Node* MaxNode::build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn) {
1136   bool is_int = gvn.type(a)->isa_int();
1137   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1138   assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs");
1139   BasicType bt = is_int ? T_INT: T_LONG;
1140   Node* zero = gvn.integercon(0, bt);
1141   Node* hook = nullptr;
1142   if (gvn.is_IterGVN()) {
1143     // Make sure a and b are not destroyed
1144     hook = new Node(2);
1145     hook->init_req(0, a);
1146     hook->init_req(1, b);
1147   }
1148   Node* cmp = nullptr;
1149   if (is_max) {
1150     cmp = gvn.transform(CmpNode::make(a, b, bt, false));
1151   } else {
1152     cmp = gvn.transform(CmpNode::make(b, a, bt, false));
1153   }
1154   Node* sub = gvn.transform(SubNode::make(a, b, bt));
1155   Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1156   Node* res = gvn.transform(CMoveNode::make(nullptr, bol, sub, zero, t));
1157   if (hook != nullptr) {
1158     hook->destruct(&gvn);
1159   }
1160   return res;
1161 }
1162 
1163 // Check if addition of an integer with type 't' and a constant 'c' can overflow.
1164 static bool can_overflow(const TypeInt* t, jint c) {
1165   jint t_lo = t->_lo;
1166   jint t_hi = t->_hi;
1167   return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1168           (c > 0 && (java_add(t_hi, c) < t_hi)));
1169 }
1170 
1171 // Let <x, x_off> = x_operands and <y, y_off> = y_operands.
1172 // If x == y and neither add(x, x_off) nor add(y, y_off) overflow, return
1173 // add(x, op(x_off, y_off)). Otherwise, return nullptr.
1174 Node* MaxNode::extract_add(PhaseGVN* phase, ConstAddOperands x_operands, ConstAddOperands y_operands) {
1175   Node* x = x_operands.first;
1176   Node* y = y_operands.first;
1177   int opcode = Opcode();
1178   assert(opcode == Op_MaxI || opcode == Op_MinI, "Unexpected opcode");
1179   const TypeInt* tx = phase->type(x)->isa_int();
1180   jint x_off = x_operands.second;
1181   jint y_off = y_operands.second;
1182   if (x == y && tx != nullptr &&
1183       !can_overflow(tx, x_off) &&
1184       !can_overflow(tx, y_off)) {
1185     jint c = opcode == Op_MinI ? MIN2(x_off, y_off) : MAX2(x_off, y_off);
1186     return new AddINode(x, phase->intcon(c));
1187   }
1188   return nullptr;
1189 }
1190 
1191 // Try to cast n as an integer addition with a constant. Return:
1192 //   <x, C>,       if n == add(x, C), where 'C' is a non-TOP constant;
1193 //   <nullptr, 0>, if n == add(x, C), where 'C' is a TOP constant; or
1194 //   <n, 0>,       otherwise.
1195 static ConstAddOperands as_add_with_constant(Node* n) {
1196   if (n->Opcode() != Op_AddI) {
1197     return ConstAddOperands(n, 0);
1198   }
1199   Node* x = n->in(1);
1200   Node* c = n->in(2);
1201   if (!c->is_Con()) {
1202     return ConstAddOperands(n, 0);
1203   }
1204   const Type* c_type = c->bottom_type();
1205   if (c_type == Type::TOP) {
1206     return ConstAddOperands(nullptr, 0);
1207   }
1208   return ConstAddOperands(x, c_type->is_int()->get_con());
1209 }
1210 
1211 Node* MaxNode::IdealI(PhaseGVN* phase, bool can_reshape) {
1212   int opcode = Opcode();
1213   assert(opcode == Op_MinI || opcode == Op_MaxI, "Unexpected opcode");
1214   // Try to transform the following pattern, in any of its four possible
1215   // permutations induced by op's commutativity:
1216   //     op(op(add(inner, inner_off), inner_other), add(outer, outer_off))
1217   // into
1218   //     op(add(inner, op(inner_off, outer_off)), inner_other),
1219   // where:
1220   //     op is either MinI or MaxI, and
1221   //     inner == outer, and
1222   //     the additions cannot overflow.
1223   for (uint inner_op_index = 1; inner_op_index <= 2; inner_op_index++) {
1224     if (in(inner_op_index)->Opcode() != opcode) {
1225       continue;
1226     }
1227     Node* outer_add = in(inner_op_index == 1 ? 2 : 1);
1228     ConstAddOperands outer_add_operands = as_add_with_constant(outer_add);
1229     if (outer_add_operands.first == nullptr) {
1230       return nullptr; // outer_add has a TOP input, no need to continue.
1231     }
1232     // One operand is a MinI/MaxI and the other is an integer addition with
1233     // constant. Test the operands of the inner MinI/MaxI.
1234     for (uint inner_add_index = 1; inner_add_index <= 2; inner_add_index++) {
1235       Node* inner_op = in(inner_op_index);
1236       Node* inner_add = inner_op->in(inner_add_index);
1237       ConstAddOperands inner_add_operands = as_add_with_constant(inner_add);
1238       if (inner_add_operands.first == nullptr) {
1239         return nullptr; // inner_add has a TOP input, no need to continue.
1240       }
1241       // Try to extract the inner add.
1242       Node* add_extracted = extract_add(phase, inner_add_operands, outer_add_operands);
1243       if (add_extracted == nullptr) {
1244         continue;
1245       }
1246       Node* add_transformed = phase->transform(add_extracted);
1247       Node* inner_other = inner_op->in(inner_add_index == 1 ? 2 : 1);
1248       return build_min_max_int(add_transformed, inner_other, opcode == Op_MaxI);
1249     }
1250   }
1251   // Try to transform
1252   //     op(add(x, x_off), add(y, y_off))
1253   // into
1254   //     add(x, op(x_off, y_off)),
1255   // where:
1256   //     op is either MinI or MaxI, and
1257   //     inner == outer, and
1258   //     the additions cannot overflow.
1259   ConstAddOperands xC = as_add_with_constant(in(1));
1260   ConstAddOperands yC = as_add_with_constant(in(2));
1261   if (xC.first == nullptr || yC.first == nullptr) return nullptr;
1262   return extract_add(phase, xC, yC);
1263 }
1264 
1265 // Ideal transformations for MaxINode
1266 Node* MaxINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1267   return IdealI(phase, can_reshape);
1268 }
1269 
1270 //=============================================================================
1271 //------------------------------add_ring---------------------------------------
1272 // Supplied function returns the sum of the inputs.
1273 const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
1274   const TypeInt *r0 = t0->is_int(); // Handy access
1275   const TypeInt *r1 = t1->is_int();
1276 
1277   // Otherwise just MAX them bits.
1278   return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1279 }
1280 
1281 //=============================================================================
1282 //------------------------------Idealize---------------------------------------
1283 // MINs show up in range-check loop limit calculations.  Look for
1284 // "MIN2(x+c0,MIN2(y,x+c1))".  Pick the smaller constant: "MIN2(x+c0,y)"
1285 Node* MinINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1286   return IdealI(phase, can_reshape);
1287 }
1288 
1289 //------------------------------add_ring---------------------------------------
1290 // Supplied function returns the sum of the inputs.
1291 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
1292   const TypeInt *r0 = t0->is_int(); // Handy access
1293   const TypeInt *r1 = t1->is_int();
1294 
1295   // Otherwise just MIN them bits.
1296   return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1297 }
1298 
1299 // Collapse the "addition with overflow-protection" pattern, and the symmetrical
1300 // "subtraction with underflow-protection" pattern. These are created during the
1301 // unrolling, when we have to adjust the limit by subtracting the stride, but want
1302 // to protect against underflow: MaxL(SubL(limit, stride), min_jint).
1303 // If we have more than one of those in a sequence:
1304 //
1305 //   x  con2
1306 //   |  |
1307 //   AddL  clamp2
1308 //     |    |
1309 //    Max/MinL con1
1310 //          |  |
1311 //          AddL  clamp1
1312 //            |    |
1313 //           Max/MinL (n)
1314 //
1315 // We want to collapse it to:
1316 //
1317 //   x  con1  con2
1318 //   |    |    |
1319 //   |   AddLNode (new_con)
1320 //   |    |
1321 //  AddLNode  clamp1
1322 //        |    |
1323 //       Max/MinL (n)
1324 //
1325 // Note: we assume that SubL was already replaced by an AddL, and that the stride
1326 // has its sign flipped: SubL(limit, stride) -> AddL(limit, -stride).
1327 static Node* fold_subI_no_underflow_pattern(Node* n, PhaseGVN* phase) {
1328   assert(n->Opcode() == Op_MaxL || n->Opcode() == Op_MinL, "sanity");
1329   // Check that the two clamps have the correct values.
1330   jlong clamp = (n->Opcode() == Op_MaxL) ? min_jint : max_jint;
1331   auto is_clamp = [&](Node* c) {
1332     const TypeLong* t = phase->type(c)->isa_long();
1333     return t != nullptr && t->is_con() &&
1334            t->get_con() == clamp;
1335   };
1336   // Check that the constants are negative if MaxL, and positive if MinL.
1337   auto is_sub_con = [&](Node* c) {
1338     const TypeLong* t = phase->type(c)->isa_long();
1339     return t != nullptr && t->is_con() &&
1340            t->get_con() < max_jint && t->get_con() > min_jint &&
1341            (t->get_con() < 0) == (n->Opcode() == Op_MaxL);
1342   };
1343   // Verify the graph level by level:
1344   Node* add1   = n->in(1);
1345   Node* clamp1 = n->in(2);
1346   if (add1->Opcode() == Op_AddL && is_clamp(clamp1)) {
1347     Node* max2 = add1->in(1);
1348     Node* con1 = add1->in(2);
1349     if (max2->Opcode() == n->Opcode() && is_sub_con(con1)) {
1350       Node* add2   = max2->in(1);
1351       Node* clamp2 = max2->in(2);
1352       if (add2->Opcode() == Op_AddL && is_clamp(clamp2)) {
1353         Node* x    = add2->in(1);
1354         Node* con2 = add2->in(2);
1355         if (is_sub_con(con2)) {
1356           Node* new_con = phase->transform(new AddLNode(con1, con2));
1357           Node* new_sub = phase->transform(new AddLNode(x, new_con));
1358           n->set_req_X(1, new_sub, phase);
1359           return n;
1360         }
1361       }
1362     }
1363   }
1364   return nullptr;
1365 }
1366 
1367 const Type* MaxLNode::add_ring(const Type* t0, const Type* t1) const {
1368   const TypeLong* r0 = t0->is_long();
1369   const TypeLong* r1 = t1->is_long();
1370 
1371   return TypeLong::make(MAX2(r0->_lo, r1->_lo), MAX2(r0->_hi, r1->_hi), MAX2(r0->_widen, r1->_widen));
1372 }
1373 
1374 Node* MaxLNode::Identity(PhaseGVN* phase) {
1375   const TypeLong* t1 = phase->type(in(1))->is_long();
1376   const TypeLong* t2 = phase->type(in(2))->is_long();
1377 
1378   // Can we determine maximum statically?
1379   if (t1->_lo >= t2->_hi) {
1380     return in(1);
1381   } else if (t2->_lo >= t1->_hi) {
1382     return in(2);
1383   }
1384 
1385   return MaxNode::Identity(phase);
1386 }
1387 
1388 Node* MaxLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1389   Node* n = AddNode::Ideal(phase, can_reshape);
1390   if (n != nullptr) {
1391     return n;
1392   }
1393   if (can_reshape) {
1394     return fold_subI_no_underflow_pattern(this, phase);
1395   }
1396   return nullptr;
1397 }
1398 
1399 const Type* MinLNode::add_ring(const Type* t0, const Type* t1) const {
1400   const TypeLong* r0 = t0->is_long();
1401   const TypeLong* r1 = t1->is_long();
1402 
1403   return TypeLong::make(MIN2(r0->_lo, r1->_lo), MIN2(r0->_hi, r1->_hi), MIN2(r0->_widen, r1->_widen));
1404 }
1405 
1406 Node* MinLNode::Identity(PhaseGVN* phase) {
1407   const TypeLong* t1 = phase->type(in(1))->is_long();
1408   const TypeLong* t2 = phase->type(in(2))->is_long();
1409 
1410   // Can we determine minimum statically?
1411   if (t1->_lo >= t2->_hi) {
1412     return in(2);
1413   } else if (t2->_lo >= t1->_hi) {
1414     return in(1);
1415   }
1416 
1417   return MaxNode::Identity(phase);
1418 }
1419 
1420 Node* MinLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1421   Node* n = AddNode::Ideal(phase, can_reshape);
1422   if (n != nullptr) {
1423     return n;
1424   }
1425   if (can_reshape) {
1426     return fold_subI_no_underflow_pattern(this, phase);
1427   }
1428   return nullptr;
1429 }
1430 
1431 //------------------------------add_ring---------------------------------------
1432 const Type* MinFNode::add_ring(const Type* t0, const Type* t1 ) const {
1433   const TypeF* r0 = t0->isa_float_constant();
1434   const TypeF* r1 = t1->isa_float_constant();
1435   if (r0 == nullptr || r1 == nullptr) {
1436     return bottom_type();
1437   }
1438 
1439   if (r0->is_nan()) {
1440     return r0;
1441   }
1442   if (r1->is_nan()) {
1443     return r1;
1444   }
1445 
1446   float f0 = r0->getf();
1447   float f1 = r1->getf();
1448   if (f0 != 0.0f || f1 != 0.0f) {
1449     return f0 < f1 ? r0 : r1;
1450   }
1451 
1452   // handle min of 0.0, -0.0 case.
1453   return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1;
1454 }
1455 
1456 //------------------------------add_ring---------------------------------------
1457 const Type* MinDNode::add_ring(const Type* t0, const Type* t1) const {
1458   const TypeD* r0 = t0->isa_double_constant();
1459   const TypeD* r1 = t1->isa_double_constant();
1460   if (r0 == nullptr || r1 == nullptr) {
1461     return bottom_type();
1462   }
1463 
1464   if (r0->is_nan()) {
1465     return r0;
1466   }
1467   if (r1->is_nan()) {
1468     return r1;
1469   }
1470 
1471   double d0 = r0->getd();
1472   double d1 = r1->getd();
1473   if (d0 != 0.0 || d1 != 0.0) {
1474     return d0 < d1 ? r0 : r1;
1475   }
1476 
1477   // handle min of 0.0, -0.0 case.
1478   return (jlong_cast(d0) < jlong_cast(d1)) ? r0 : r1;
1479 }
1480 
1481 //------------------------------add_ring---------------------------------------
1482 const Type* MaxFNode::add_ring(const Type* t0, const Type* t1) const {
1483   const TypeF* r0 = t0->isa_float_constant();
1484   const TypeF* r1 = t1->isa_float_constant();
1485   if (r0 == nullptr || r1 == nullptr) {
1486     return bottom_type();
1487   }
1488 
1489   if (r0->is_nan()) {
1490     return r0;
1491   }
1492   if (r1->is_nan()) {
1493     return r1;
1494   }
1495 
1496   float f0 = r0->getf();
1497   float f1 = r1->getf();
1498   if (f0 != 0.0f || f1 != 0.0f) {
1499     return f0 > f1 ? r0 : r1;
1500   }
1501 
1502   // handle max of 0.0,-0.0 case.
1503   return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1;
1504 }
1505 
1506 //------------------------------add_ring---------------------------------------
1507 const Type* MaxDNode::add_ring(const Type* t0, const Type* t1) const {
1508   const TypeD* r0 = t0->isa_double_constant();
1509   const TypeD* r1 = t1->isa_double_constant();
1510   if (r0 == nullptr || r1 == nullptr) {
1511     return bottom_type();
1512   }
1513 
1514   if (r0->is_nan()) {
1515     return r0;
1516   }
1517   if (r1->is_nan()) {
1518     return r1;
1519   }
1520 
1521   double d0 = r0->getd();
1522   double d1 = r1->getd();
1523   if (d0 != 0.0 || d1 != 0.0) {
1524     return d0 > d1 ? r0 : r1;
1525   }
1526 
1527   // handle max of 0.0, -0.0 case.
1528   return (jlong_cast(d0) > jlong_cast(d1)) ? r0 : r1;
1529 }