1 /*
   2  * Copyright (c) 1997, 2023, 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 //=============================================================================
 257 //------------------------------Idealize---------------------------------------
 258 Node* AddNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) {
 259   Node* in1 = in(1);
 260   Node* in2 = in(2);
 261   int op1 = in1->Opcode();
 262   int op2 = in2->Opcode();
 263   // Fold (con1-x)+con2 into (con1+con2)-x
 264   if (op1 == Op_Add(bt) && op2 == Op_Sub(bt)) {
 265     // Swap edges to try optimizations below
 266     in1 = in2;
 267     in2 = in(1);
 268     op1 = op2;
 269     op2 = in2->Opcode();
 270   }
 271   if (op1 == Op_Sub(bt)) {
 272     const Type* t_sub1 = phase->type(in1->in(1));
 273     const Type* t_2    = phase->type(in2       );
 274     if (t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP) {
 275       return SubNode::make(phase->makecon(add_ring(t_sub1, t_2)), in1->in(2), bt);
 276     }
 277     // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
 278     if (op2 == Op_Sub(bt)) {
 279       // Check for dead cycle: d = (a-b)+(c-d)
 280       assert( in1->in(2) != this && in2->in(2) != this,
 281               "dead loop in AddINode::Ideal" );
 282       Node* sub = SubNode::make(nullptr, nullptr, bt);
 283       sub->init_req(1, phase->transform(AddNode::make(in1->in(1), in2->in(1), bt)));
 284       sub->init_req(2, phase->transform(AddNode::make(in1->in(2), in2->in(2), bt)));
 285       return sub;
 286     }
 287     // Convert "(a-b)+(b+c)" into "(a+c)"
 288     if (op2 == Op_Add(bt) && in1->in(2) == in2->in(1)) {
 289       assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal/AddLNode::Ideal");
 290       return AddNode::make(in1->in(1), in2->in(2), bt);
 291     }
 292     // Convert "(a-b)+(c+b)" into "(a+c)"
 293     if (op2 == Op_Add(bt) && in1->in(2) == in2->in(2)) {
 294       assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal/AddLNode::Ideal");
 295       return AddNode::make(in1->in(1), in2->in(1), bt);
 296     }
 297   }
 298 
 299   // Convert (con - y) + x into "(x - y) + con"
 300   if (op1 == Op_Sub(bt) && in1->in(1)->Opcode() == Op_ConIL(bt)
 301       && in1 != in1->in(2) && !(in1->in(2)->is_Phi() && in1->in(2)->as_Phi()->is_tripcount(bt))) {
 302     return AddNode::make(phase->transform(SubNode::make(in2, in1->in(2), bt)), in1->in(1), bt);
 303   }
 304 
 305   // Convert x + (con - y) into "(x - y) + con"
 306   if (op2 == Op_Sub(bt) && in2->in(1)->Opcode() == Op_ConIL(bt)
 307       && in2 != in2->in(2) && !(in2->in(2)->is_Phi() && in2->in(2)->as_Phi()->is_tripcount(bt))) {
 308     return AddNode::make(phase->transform(SubNode::make(in1, in2->in(2), bt)), in2->in(1), bt);
 309   }
 310 
 311   // Associative
 312   if (op1 == Op_Mul(bt) && op2 == Op_Mul(bt)) {
 313     Node* add_in1 = nullptr;
 314     Node* add_in2 = nullptr;
 315     Node* mul_in = nullptr;
 316 
 317     if (in1->in(1) == in2->in(1)) {
 318       // Convert "a*b+a*c into a*(b+c)
 319       add_in1 = in1->in(2);
 320       add_in2 = in2->in(2);
 321       mul_in = in1->in(1);
 322     } else if (in1->in(2) == in2->in(1)) {
 323       // Convert a*b+b*c into b*(a+c)
 324       add_in1 = in1->in(1);
 325       add_in2 = in2->in(2);
 326       mul_in = in1->in(2);
 327     } else if (in1->in(2) == in2->in(2)) {
 328       // Convert a*c+b*c into (a+b)*c
 329       add_in1 = in1->in(1);
 330       add_in2 = in2->in(1);
 331       mul_in = in1->in(2);
 332     } else if (in1->in(1) == in2->in(2)) {
 333       // Convert a*b+c*a into a*(b+c)
 334       add_in1 = in1->in(2);
 335       add_in2 = in2->in(1);
 336       mul_in = in1->in(1);
 337     }
 338 
 339     if (mul_in != nullptr) {
 340       Node* add = phase->transform(AddNode::make(add_in1, add_in2, bt));
 341       return MulNode::make(mul_in, add, bt);
 342     }
 343   }
 344 
 345   // Convert (x >>> rshift) + (x << lshift) into RotateRight(x, rshift)
 346   if (Matcher::match_rule_supported(Op_RotateRight) &&
 347       ((op1 == Op_URShift(bt) && op2 == Op_LShift(bt)) || (op1 == Op_LShift(bt) && op2 == Op_URShift(bt))) &&
 348       in1->in(1) != nullptr && in1->in(1) == in2->in(1)) {
 349     Node* rshift = op1 == Op_URShift(bt) ? in1->in(2) : in2->in(2);
 350     Node* lshift = op1 == Op_URShift(bt) ? in2->in(2) : in1->in(2);
 351     if (rshift != nullptr && lshift != nullptr) {
 352       const TypeInt* rshift_t = phase->type(rshift)->isa_int();
 353       const TypeInt* lshift_t = phase->type(lshift)->isa_int();
 354       int bits = bt == T_INT ? 32 : 64;
 355       int mask = bt == T_INT ? 0x1F : 0x3F;
 356       if (lshift_t != nullptr && lshift_t->is_con() &&
 357           rshift_t != nullptr && rshift_t->is_con() &&
 358           ((lshift_t->get_con() & mask) == (bits - (rshift_t->get_con() & mask)))) {
 359         return new RotateRightNode(in1->in(1), phase->intcon(rshift_t->get_con() & mask), TypeInteger::bottom(bt));
 360       }
 361     }
 362   }
 363 
 364   return AddNode::Ideal(phase, can_reshape);
 365 }
 366 
 367 
 368 Node* AddINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 369   Node* in1 = in(1);
 370   Node* in2 = in(2);
 371   int op1 = in1->Opcode();
 372   int op2 = in2->Opcode();
 373 
 374   // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
 375   // Helps with array allocation math constant folding
 376   // See 4790063:
 377   // Unrestricted transformation is unsafe for some runtime values of 'x'
 378   // ( x ==  0, z == 1, y == -1 ) fails
 379   // ( x == -5, z == 1, y ==  1 ) fails
 380   // Transform works for small z and small negative y when the addition
 381   // (x + (y << z)) does not cross zero.
 382   // Implement support for negative y and (x >= -(y << z))
 383   // Have not observed cases where type information exists to support
 384   // positive y and (x <= -(y << z))
 385   if (op1 == Op_URShiftI && op2 == Op_ConI &&
 386       in1->in(2)->Opcode() == Op_ConI) {
 387     jint z = phase->type(in1->in(2))->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
 388     jint y = phase->type(in2)->is_int()->get_con();
 389 
 390     if (z < 5 && -5 < y && y < 0) {
 391       const Type* t_in11 = phase->type(in1->in(1));
 392       if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z))) {
 393         Node* a = phase->transform(new AddINode( in1->in(1), phase->intcon(y<<z)));
 394         return new URShiftINode(a, in1->in(2));
 395       }
 396     }
 397   }
 398 
 399   return AddNode::IdealIL(phase, can_reshape, T_INT);
 400 }
 401 
 402 
 403 //------------------------------Identity---------------------------------------
 404 // Fold (x-y)+y  OR  y+(x-y)  into  x
 405 Node* AddINode::Identity(PhaseGVN* phase) {
 406   if (in(1)->Opcode() == Op_SubI && in(1)->in(2) == in(2)) {
 407     return in(1)->in(1);
 408   } else if (in(2)->Opcode() == Op_SubI && in(2)->in(2) == in(1)) {
 409     return in(2)->in(1);
 410   }
 411   return AddNode::Identity(phase);
 412 }
 413 
 414 
 415 //------------------------------add_ring---------------------------------------
 416 // Supplied function returns the sum of the inputs.  Guaranteed never
 417 // to be passed a TOP or BOTTOM type, these are filtered out by
 418 // pre-check.
 419 const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {
 420   const TypeInt *r0 = t0->is_int(); // Handy access
 421   const TypeInt *r1 = t1->is_int();
 422   int lo = java_add(r0->_lo, r1->_lo);
 423   int hi = java_add(r0->_hi, r1->_hi);
 424   if( !(r0->is_con() && r1->is_con()) ) {
 425     // Not both constants, compute approximate result
 426     if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
 427       lo = min_jint; hi = max_jint; // Underflow on the low side
 428     }
 429     if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
 430       lo = min_jint; hi = max_jint; // Overflow on the high side
 431     }
 432     if( lo > hi ) {               // Handle overflow
 433       lo = min_jint; hi = max_jint;
 434     }
 435   } else {
 436     // both constants, compute precise result using 'lo' and 'hi'
 437     // Semantics define overflow and underflow for integer addition
 438     // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
 439   }
 440   return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
 441 }
 442 
 443 
 444 //=============================================================================
 445 //------------------------------Idealize---------------------------------------
 446 Node* AddLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 447   return AddNode::IdealIL(phase, can_reshape, T_LONG);
 448 }
 449 
 450 
 451 //------------------------------Identity---------------------------------------
 452 // Fold (x-y)+y  OR  y+(x-y)  into  x
 453 Node* AddLNode::Identity(PhaseGVN* phase) {
 454   if (in(1)->Opcode() == Op_SubL && in(1)->in(2) == in(2)) {
 455     return in(1)->in(1);
 456   } else if (in(2)->Opcode() == Op_SubL && in(2)->in(2) == in(1)) {
 457     return in(2)->in(1);
 458   }
 459   return AddNode::Identity(phase);
 460 }
 461 
 462 
 463 //------------------------------add_ring---------------------------------------
 464 // Supplied function returns the sum of the inputs.  Guaranteed never
 465 // to be passed a TOP or BOTTOM type, these are filtered out by
 466 // pre-check.
 467 const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {
 468   const TypeLong *r0 = t0->is_long(); // Handy access
 469   const TypeLong *r1 = t1->is_long();
 470   jlong lo = java_add(r0->_lo, r1->_lo);
 471   jlong hi = java_add(r0->_hi, r1->_hi);
 472   if( !(r0->is_con() && r1->is_con()) ) {
 473     // Not both constants, compute approximate result
 474     if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
 475       lo =min_jlong; hi = max_jlong; // Underflow on the low side
 476     }
 477     if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
 478       lo = min_jlong; hi = max_jlong; // Overflow on the high side
 479     }
 480     if( lo > hi ) {               // Handle overflow
 481       lo = min_jlong; hi = max_jlong;
 482     }
 483   } else {
 484     // both constants, compute precise result using 'lo' and 'hi'
 485     // Semantics define overflow and underflow for integer addition
 486     // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
 487   }
 488   return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
 489 }
 490 
 491 
 492 //=============================================================================
 493 //------------------------------add_of_identity--------------------------------
 494 // Check for addition of the identity
 495 const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {
 496   // x ADD 0  should return x unless 'x' is a -zero
 497   //
 498   // const Type *zero = add_id();     // The additive identity
 499   // jfloat f1 = t1->getf();
 500   // jfloat f2 = t2->getf();
 501   //
 502   // if( t1->higher_equal( zero ) ) return t2;
 503   // if( t2->higher_equal( zero ) ) return t1;
 504 
 505   return nullptr;
 506 }
 507 
 508 //------------------------------add_ring---------------------------------------
 509 // Supplied function returns the sum of the inputs.
 510 // This also type-checks the inputs for sanity.  Guaranteed never to
 511 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 512 const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {
 513   if (!t0->isa_float_constant() || !t1->isa_float_constant()) {
 514     return bottom_type();
 515   }
 516   return TypeF::make( t0->getf() + t1->getf() );
 517 }
 518 
 519 //------------------------------Ideal------------------------------------------
 520 Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 521   // Floating point additions are not associative because of boundary conditions (infinity)
 522   return commute(phase, this) ? this : nullptr;
 523 }
 524 
 525 
 526 //=============================================================================
 527 //------------------------------add_of_identity--------------------------------
 528 // Check for addition of the identity
 529 const Type *AddDNode::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 //------------------------------add_ring---------------------------------------
 542 // Supplied function returns the sum of the inputs.
 543 // This also type-checks the inputs for sanity.  Guaranteed never to
 544 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 545 const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {
 546   if (!t0->isa_double_constant() || !t1->isa_double_constant()) {
 547     return bottom_type();
 548   }
 549   return TypeD::make( t0->getd() + t1->getd() );
 550 }
 551 
 552 //------------------------------Ideal------------------------------------------
 553 Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 554   // Floating point additions are not associative because of boundary conditions (infinity)
 555   return commute(phase, this) ? this : nullptr;
 556 }
 557 
 558 
 559 //=============================================================================
 560 //------------------------------Identity---------------------------------------
 561 // If one input is a constant 0, return the other input.
 562 Node* AddPNode::Identity(PhaseGVN* phase) {
 563   return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;
 564 }
 565 
 566 //------------------------------Idealize---------------------------------------
 567 Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 568   // Bail out if dead inputs
 569   if( phase->type( in(Address) ) == Type::TOP ) return nullptr;
 570 
 571   // If the left input is an add of a constant, flatten the expression tree.
 572   const Node *n = in(Address);
 573   if (n->is_AddP() && n->in(Base) == in(Base)) {
 574     const AddPNode *addp = n->as_AddP(); // Left input is an AddP
 575     assert( !addp->in(Address)->is_AddP() ||
 576              addp->in(Address)->as_AddP() != addp,
 577             "dead loop in AddPNode::Ideal" );
 578     // Type of left input's right input
 579     const Type *t = phase->type( addp->in(Offset) );
 580     if( t == Type::TOP ) return nullptr;
 581     const TypeX *t12 = t->is_intptr_t();
 582     if( t12->is_con() ) {       // Left input is an add of a constant?
 583       // If the right input is a constant, combine constants
 584       const Type *temp_t2 = phase->type( in(Offset) );
 585       if( temp_t2 == Type::TOP ) return nullptr;
 586       const TypeX *t2 = temp_t2->is_intptr_t();
 587       Node* address;
 588       Node* offset;
 589       if( t2->is_con() ) {
 590         // The Add of the flattened expression
 591         address = addp->in(Address);
 592         offset  = phase->MakeConX(t2->get_con() + t12->get_con());
 593       } else {
 594         // Else move the constant to the right.  ((A+con)+B) into ((A+B)+con)
 595         address = phase->transform(new AddPNode(in(Base),addp->in(Address),in(Offset)));
 596         offset  = addp->in(Offset);
 597       }
 598       set_req_X(Address, address, phase);
 599       set_req_X(Offset, offset, phase);
 600       return this;
 601     }
 602   }
 603 
 604   // Raw pointers?
 605   if( in(Base)->bottom_type() == Type::TOP ) {
 606     // If this is a null+long form (from unsafe accesses), switch to a rawptr.
 607     if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
 608       Node* offset = in(Offset);
 609       return new CastX2PNode(offset);
 610     }
 611   }
 612 
 613   // If the right is an add of a constant, push the offset down.
 614   // Convert: (ptr + (offset+con)) into (ptr+offset)+con.
 615   // The idea is to merge array_base+scaled_index groups together,
 616   // and only have different constant offsets from the same base.
 617   const Node *add = in(Offset);
 618   if( add->Opcode() == Op_AddX && add->in(1) != add ) {
 619     const Type *t22 = phase->type( add->in(2) );
 620     if( t22->singleton() && (t22 != Type::TOP) ) {  // Right input is an add of a constant?
 621       set_req(Address, phase->transform(new AddPNode(in(Base),in(Address),add->in(1))));
 622       set_req_X(Offset, add->in(2), phase); // puts add on igvn worklist if needed
 623       return this;              // Made progress
 624     }
 625   }
 626 
 627   return nullptr;                  // No progress
 628 }
 629 
 630 //------------------------------bottom_type------------------------------------
 631 // Bottom-type is the pointer-type with unknown offset.
 632 const Type *AddPNode::bottom_type() const {
 633   if (in(Address) == nullptr)  return TypePtr::BOTTOM;
 634   const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
 635   if( !tp ) return Type::TOP;   // TOP input means TOP output
 636   assert( in(Offset)->Opcode() != Op_ConP, "" );
 637   const Type *t = in(Offset)->bottom_type();
 638   if( t == Type::TOP )
 639     return tp->add_offset(Type::OffsetTop);
 640   const TypeX *tx = t->is_intptr_t();
 641   intptr_t txoffset = Type::OffsetBot;
 642   if (tx->is_con()) {   // Left input is an add of a constant?
 643     txoffset = tx->get_con();
 644   }
 645   if (tp->isa_aryptr()) {
 646     // In the case of a flat inline type array, each field has its
 647     // own slice so we need to extract the field being accessed from
 648     // the address computation
 649     return tp->is_aryptr()->add_field_offset_and_offset(txoffset);
 650   }
 651   return tp->add_offset(txoffset);
 652 }
 653 
 654 //------------------------------Value------------------------------------------
 655 const Type* AddPNode::Value(PhaseGVN* phase) const {
 656   // Either input is TOP ==> the result is TOP
 657   const Type *t1 = phase->type( in(Address) );
 658   const Type *t2 = phase->type( in(Offset) );
 659   if( t1 == Type::TOP ) return Type::TOP;
 660   if( t2 == Type::TOP ) return Type::TOP;
 661 
 662   // Left input is a pointer
 663   const TypePtr *p1 = t1->isa_ptr();
 664   // Right input is an int
 665   const TypeX *p2 = t2->is_intptr_t();
 666   // Add 'em
 667   intptr_t p2offset = Type::OffsetBot;
 668   if (p2->is_con()) {   // Left input is an add of a constant?
 669     p2offset = p2->get_con();
 670   }
 671   if (p1->isa_aryptr()) {
 672     // In the case of a flat inline type array, each field has its
 673     // own slice so we need to extract the field being accessed from
 674     // the address computation
 675     return p1->is_aryptr()->add_field_offset_and_offset(p2offset);
 676   }
 677   return p1->add_offset(p2offset);
 678 }
 679 
 680 //------------------------Ideal_base_and_offset--------------------------------
 681 // Split an oop pointer into a base and offset.
 682 // (The offset might be Type::OffsetBot in the case of an array.)
 683 // Return the base, or null if failure.
 684 Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseValues* phase,
 685                                       // second return value:
 686                                       intptr_t& offset) {
 687   if (ptr->is_AddP()) {
 688     Node* base = ptr->in(AddPNode::Base);
 689     Node* addr = ptr->in(AddPNode::Address);
 690     Node* offs = ptr->in(AddPNode::Offset);
 691     if (base == addr || base->is_top()) {
 692       offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
 693       if (offset != Type::OffsetBot) {
 694         return addr;
 695       }
 696     }
 697   }
 698   offset = Type::OffsetBot;
 699   return nullptr;
 700 }
 701 
 702 //------------------------------unpack_offsets----------------------------------
 703 // Collect the AddP offset values into the elements array, giving up
 704 // if there are more than length.
 705 int AddPNode::unpack_offsets(Node* elements[], int length) {
 706   int count = 0;
 707   Node* addr = this;
 708   Node* base = addr->in(AddPNode::Base);
 709   while (addr->is_AddP()) {
 710     if (addr->in(AddPNode::Base) != base) {
 711       // give up
 712       return -1;
 713     }
 714     elements[count++] = addr->in(AddPNode::Offset);
 715     if (count == length) {
 716       // give up
 717       return -1;
 718     }
 719     addr = addr->in(AddPNode::Address);
 720   }
 721   if (addr != base) {
 722     return -1;
 723   }
 724   return count;
 725 }
 726 
 727 //------------------------------match_edge-------------------------------------
 728 // Do we Match on this edge index or not?  Do not match base pointer edge
 729 uint AddPNode::match_edge(uint idx) const {
 730   return idx > Base;
 731 }
 732 
 733 //=============================================================================
 734 //------------------------------Identity---------------------------------------
 735 Node* OrINode::Identity(PhaseGVN* phase) {
 736   // x | x => x
 737   if (in(1) == in(2)) {
 738     return in(1);
 739   }
 740 
 741   return AddNode::Identity(phase);
 742 }
 743 
 744 // Find shift value for Integer or Long OR.
 745 Node* rotate_shift(PhaseGVN* phase, Node* lshift, Node* rshift, int mask) {
 746   // val << norm_con_shift | val >> ({32|64} - norm_con_shift) => rotate_left val, norm_con_shift
 747   const TypeInt* lshift_t = phase->type(lshift)->isa_int();
 748   const TypeInt* rshift_t = phase->type(rshift)->isa_int();
 749   if (lshift_t != nullptr && lshift_t->is_con() &&
 750       rshift_t != nullptr && rshift_t->is_con() &&
 751       ((lshift_t->get_con() & mask) == ((mask + 1) - (rshift_t->get_con() & mask)))) {
 752     return phase->intcon(lshift_t->get_con() & mask);
 753   }
 754   // val << var_shift | val >> ({0|32|64} - var_shift) => rotate_left val, var_shift
 755   if (rshift->Opcode() == Op_SubI && rshift->in(2) == lshift && rshift->in(1)->is_Con()){
 756     const TypeInt* shift_t = phase->type(rshift->in(1))->isa_int();
 757     if (shift_t != nullptr && shift_t->is_con() &&
 758         (shift_t->get_con() == 0 || shift_t->get_con() == (mask + 1))) {
 759       return lshift;
 760     }
 761   }
 762   return nullptr;
 763 }
 764 
 765 Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 766   int lopcode = in(1)->Opcode();
 767   int ropcode = in(2)->Opcode();
 768   if (Matcher::match_rule_supported(Op_RotateLeft) &&
 769       lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) {
 770     Node* lshift = in(1)->in(2);
 771     Node* rshift = in(2)->in(2);
 772     Node* shift = rotate_shift(phase, lshift, rshift, 0x1F);
 773     if (shift != nullptr) {
 774       return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT);
 775     }
 776     return nullptr;
 777   }
 778   if (Matcher::match_rule_supported(Op_RotateRight) &&
 779       lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) {
 780     Node* rshift = in(1)->in(2);
 781     Node* lshift = in(2)->in(2);
 782     Node* shift = rotate_shift(phase, rshift, lshift, 0x1F);
 783     if (shift != nullptr) {
 784       return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
 785     }
 786   }
 787   return nullptr;
 788 }
 789 
 790 //------------------------------add_ring---------------------------------------
 791 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
 792 // the logical operations the ring's ADD is really a logical OR function.
 793 // This also type-checks the inputs for sanity.  Guaranteed never to
 794 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 795 const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
 796   const TypeInt *r0 = t0->is_int(); // Handy access
 797   const TypeInt *r1 = t1->is_int();
 798 
 799   // If both args are bool, can figure out better types
 800   if ( r0 == TypeInt::BOOL ) {
 801     if ( r1 == TypeInt::ONE) {
 802       return TypeInt::ONE;
 803     } else if ( r1 == TypeInt::BOOL ) {
 804       return TypeInt::BOOL;
 805     }
 806   } else if ( r0 == TypeInt::ONE ) {
 807     if ( r1 == TypeInt::BOOL ) {
 808       return TypeInt::ONE;
 809     }
 810   }
 811 
 812   // If either input is not a constant, just return all integers.
 813   if( !r0->is_con() || !r1->is_con() )
 814     return TypeInt::INT;        // Any integer, but still no symbols.
 815 
 816   // Otherwise just OR them bits.
 817   return TypeInt::make( r0->get_con() | r1->get_con() );
 818 }
 819 
 820 //=============================================================================
 821 //------------------------------Identity---------------------------------------
 822 Node* OrLNode::Identity(PhaseGVN* phase) {
 823   // x | x => x
 824   if (in(1) == in(2)) {
 825     return in(1);
 826   }
 827 
 828   return AddNode::Identity(phase);
 829 }
 830 
 831 Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 832   int lopcode = in(1)->Opcode();
 833   int ropcode = in(2)->Opcode();
 834   if (Matcher::match_rule_supported(Op_RotateLeft) &&
 835       lopcode == Op_LShiftL && ropcode == Op_URShiftL && in(1)->in(1) == in(2)->in(1)) {
 836     Node* lshift = in(1)->in(2);
 837     Node* rshift = in(2)->in(2);
 838     Node* shift = rotate_shift(phase, lshift, rshift, 0x3F);
 839     if (shift != nullptr) {
 840       return new RotateLeftNode(in(1)->in(1), shift, TypeLong::LONG);
 841     }
 842     return nullptr;
 843   }
 844   if (Matcher::match_rule_supported(Op_RotateRight) &&
 845       lopcode == Op_URShiftL && ropcode == Op_LShiftL && in(1)->in(1) == in(2)->in(1)) {
 846     Node* rshift = in(1)->in(2);
 847     Node* lshift = in(2)->in(2);
 848     Node* shift = rotate_shift(phase, rshift, lshift, 0x3F);
 849     if (shift != nullptr) {
 850       return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG);
 851     }
 852   }
 853   return nullptr;
 854 }
 855 
 856 //------------------------------add_ring---------------------------------------
 857 const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
 858   const TypeLong *r0 = t0->is_long(); // Handy access
 859   const TypeLong *r1 = t1->is_long();
 860 
 861   // If either input is not a constant, just return all integers.
 862   if( !r0->is_con() || !r1->is_con() )
 863     return TypeLong::LONG;      // Any integer, but still no symbols.
 864 
 865   // Otherwise just OR them bits.
 866   return TypeLong::make( r0->get_con() | r1->get_con() );
 867 }
 868 
 869 //---------------------------Helper -------------------------------------------
 870 /* Decide if the given node is used only in arithmetic expressions(add or sub).
 871  */
 872 static bool is_used_in_only_arithmetic(Node* n, BasicType bt) {
 873   for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
 874     Node* u = n->fast_out(i);
 875     if (u->Opcode() != Op_Add(bt) && u->Opcode() != Op_Sub(bt)) {
 876       return false;
 877     }
 878   }
 879   return true;
 880 }
 881 
 882 //=============================================================================
 883 //------------------------------Idealize---------------------------------------
 884 Node* XorINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 885   Node* in1 = in(1);
 886   Node* in2 = in(2);
 887 
 888   // Convert ~x into -1-x when ~x is used in an arithmetic expression
 889   // or x itself is an expression.
 890   if (phase->type(in2) == TypeInt::MINUS_1) { // follows LHS^(-1), i.e., ~LHS
 891     if (phase->is_IterGVN()) {
 892       if (is_used_in_only_arithmetic(this, T_INT)
 893           // LHS is arithmetic
 894           || (in1->Opcode() == Op_AddI || in1->Opcode() == Op_SubI)) {
 895         return new SubINode(in2, in1);
 896       }
 897     } else {
 898       // graph could be incomplete in GVN so we postpone to IGVN
 899       phase->record_for_igvn(this);
 900     }
 901   }
 902 
 903   // Propagate xor through constant cmoves. This pattern can occur after expansion of Conv2B nodes.
 904   const TypeInt* in2_type = phase->type(in2)->isa_int();
 905   if (in1->Opcode() == Op_CMoveI && in2_type != nullptr && in2_type->is_con()) {
 906     int in2_val = in2_type->get_con();
 907 
 908     // Get types of both sides of the CMove
 909     const TypeInt* left = phase->type(in1->in(CMoveNode::IfFalse))->isa_int();
 910     const TypeInt* right = phase->type(in1->in(CMoveNode::IfTrue))->isa_int();
 911 
 912     // Ensure that both sides are int constants
 913     if (left != nullptr && right != nullptr && left->is_con() && right->is_con()) {
 914       Node* cond = in1->in(CMoveNode::Condition);
 915 
 916       // Check that the comparison is a bool and that the cmp node type is correct
 917       if (cond->is_Bool()) {
 918         int cmp_op = cond->in(1)->Opcode();
 919 
 920         if (cmp_op == Op_CmpI || cmp_op == Op_CmpP) {
 921           int l_val = left->get_con();
 922           int r_val = right->get_con();
 923 
 924           return new CMoveINode(cond, phase->intcon(l_val ^ in2_val), phase->intcon(r_val ^ in2_val), TypeInt::INT);
 925         }
 926       }
 927     }
 928   }
 929 
 930   return AddNode::Ideal(phase, can_reshape);
 931 }
 932 
 933 const Type* XorINode::Value(PhaseGVN* phase) const {
 934   Node* in1 = in(1);
 935   Node* in2 = in(2);
 936   const Type* t1 = phase->type(in1);
 937   const Type* t2 = phase->type(in2);
 938   if (t1 == Type::TOP || t2 == Type::TOP) {
 939     return Type::TOP;
 940   }
 941   // x ^ x ==> 0
 942   if (in1->eqv_uncast(in2)) {
 943     return add_id();
 944   }
 945   // result of xor can only have bits sets where any of the
 946   // inputs have bits set. lo can always become 0.
 947   const TypeInt* t1i = t1->is_int();
 948   const TypeInt* t2i = t2->is_int();
 949   if ((t1i->_lo >= 0) &&
 950       (t1i->_hi > 0)  &&
 951       (t2i->_lo >= 0) &&
 952       (t2i->_hi > 0)) {
 953     // hi - set all bits below the highest bit. Using round_down to avoid overflow.
 954     const TypeInt* t1x = TypeInt::make(0, round_down_power_of_2(t1i->_hi) + (round_down_power_of_2(t1i->_hi) - 1), t1i->_widen);
 955     const TypeInt* t2x = TypeInt::make(0, round_down_power_of_2(t2i->_hi) + (round_down_power_of_2(t2i->_hi) - 1), t2i->_widen);
 956     return t1x->meet(t2x);
 957   }
 958   return AddNode::Value(phase);
 959 }
 960 
 961 
 962 //------------------------------add_ring---------------------------------------
 963 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
 964 // the logical operations the ring's ADD is really a logical OR function.
 965 // This also type-checks the inputs for sanity.  Guaranteed never to
 966 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 967 const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
 968   const TypeInt *r0 = t0->is_int(); // Handy access
 969   const TypeInt *r1 = t1->is_int();
 970 
 971   // Complementing a boolean?
 972   if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE
 973                                || r1 == TypeInt::BOOL))
 974     return TypeInt::BOOL;
 975 
 976   if( !r0->is_con() || !r1->is_con() ) // Not constants
 977     return TypeInt::INT;        // Any integer, but still no symbols.
 978 
 979   // Otherwise just XOR them bits.
 980   return TypeInt::make( r0->get_con() ^ r1->get_con() );
 981 }
 982 
 983 //=============================================================================
 984 //------------------------------add_ring---------------------------------------
 985 const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
 986   const TypeLong *r0 = t0->is_long(); // Handy access
 987   const TypeLong *r1 = t1->is_long();
 988 
 989   // If either input is not a constant, just return all integers.
 990   if( !r0->is_con() || !r1->is_con() )
 991     return TypeLong::LONG;      // Any integer, but still no symbols.
 992 
 993   // Otherwise just OR them bits.
 994   return TypeLong::make( r0->get_con() ^ r1->get_con() );
 995 }
 996 
 997 Node* XorLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 998   Node* in1 = in(1);
 999   Node* in2 = in(2);
1000 
1001   // Convert ~x into -1-x when ~x is used in an arithmetic expression
1002   // or x itself is an arithmetic expression.
1003   if (phase->type(in2) == TypeLong::MINUS_1) { // follows LHS^(-1), i.e., ~LHS
1004     if (phase->is_IterGVN()) {
1005       if (is_used_in_only_arithmetic(this, T_LONG)
1006           // LHS is arithmetic
1007           || (in1->Opcode() == Op_AddL || in1->Opcode() == Op_SubL)) {
1008         return new SubLNode(in2, in1);
1009       }
1010     } else {
1011       // graph could be incomplete in GVN so we postpone to IGVN
1012       phase->record_for_igvn(this);
1013     }
1014   }
1015   return AddNode::Ideal(phase, can_reshape);
1016 }
1017 
1018 const Type* XorLNode::Value(PhaseGVN* phase) const {
1019   Node* in1 = in(1);
1020   Node* in2 = in(2);
1021   const Type* t1 = phase->type(in1);
1022   const Type* t2 = phase->type(in2);
1023   if (t1 == Type::TOP || t2 == Type::TOP) {
1024     return Type::TOP;
1025   }
1026   // x ^ x ==> 0
1027   if (in1->eqv_uncast(in2)) {
1028     return add_id();
1029   }
1030   // result of xor can only have bits sets where any of the
1031   // inputs have bits set. lo can always become 0.
1032   const TypeLong* t1l = t1->is_long();
1033   const TypeLong* t2l = t2->is_long();
1034   if ((t1l->_lo >= 0) &&
1035       (t1l->_hi > 0)  &&
1036       (t2l->_lo >= 0) &&
1037       (t2l->_hi > 0)) {
1038     // hi - set all bits below the highest bit. Using round_down to avoid overflow.
1039     const TypeLong* t1x = TypeLong::make(0, round_down_power_of_2(t1l->_hi) + (round_down_power_of_2(t1l->_hi) - 1), t1l->_widen);
1040     const TypeLong* t2x = TypeLong::make(0, round_down_power_of_2(t2l->_hi) + (round_down_power_of_2(t2l->_hi) - 1), t2l->_widen);
1041     return t1x->meet(t2x);
1042   }
1043   return AddNode::Value(phase);
1044 }
1045 
1046 Node* build_min_max_int(Node* a, Node* b, bool is_max) {
1047   if (is_max) {
1048     return new MaxINode(a, b);
1049   } else {
1050     return new MinINode(a, b);
1051   }
1052 }
1053 
1054 Node* MaxNode::build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn) {
1055   bool is_int = gvn.type(a)->isa_int();
1056   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1057   assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs");
1058   BasicType bt = is_int ? T_INT: T_LONG;
1059   Node* hook = nullptr;
1060   if (gvn.is_IterGVN()) {
1061     // Make sure a and b are not destroyed
1062     hook = new Node(2);
1063     hook->init_req(0, a);
1064     hook->init_req(1, b);
1065   }
1066   Node* res = nullptr;
1067   if (is_int && !is_unsigned) {
1068     res = gvn.transform(build_min_max_int(a, b, is_max));
1069     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");
1070   } else {
1071     Node* cmp = nullptr;
1072     if (is_max) {
1073       cmp = gvn.transform(CmpNode::make(a, b, bt, is_unsigned));
1074     } else {
1075       cmp = gvn.transform(CmpNode::make(b, a, bt, is_unsigned));
1076     }
1077     Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1078     res = gvn.transform(CMoveNode::make(nullptr, bol, a, b, t));
1079   }
1080   if (hook != nullptr) {
1081     hook->destruct(&gvn);
1082   }
1083   return res;
1084 }
1085 
1086 Node* MaxNode::build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn) {
1087   bool is_int = gvn.type(a)->isa_int();
1088   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1089   assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs");
1090   BasicType bt = is_int ? T_INT: T_LONG;
1091   Node* zero = gvn.integercon(0, bt);
1092   Node* hook = nullptr;
1093   if (gvn.is_IterGVN()) {
1094     // Make sure a and b are not destroyed
1095     hook = new Node(2);
1096     hook->init_req(0, a);
1097     hook->init_req(1, b);
1098   }
1099   Node* cmp = nullptr;
1100   if (is_max) {
1101     cmp = gvn.transform(CmpNode::make(a, b, bt, false));
1102   } else {
1103     cmp = gvn.transform(CmpNode::make(b, a, bt, false));
1104   }
1105   Node* sub = gvn.transform(SubNode::make(a, b, bt));
1106   Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1107   Node* res = gvn.transform(CMoveNode::make(nullptr, bol, sub, zero, t));
1108   if (hook != nullptr) {
1109     hook->destruct(&gvn);
1110   }
1111   return res;
1112 }
1113 
1114 // Check if addition of an integer with type 't' and a constant 'c' can overflow.
1115 static bool can_overflow(const TypeInt* t, jint c) {
1116   jint t_lo = t->_lo;
1117   jint t_hi = t->_hi;
1118   return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1119           (c > 0 && (java_add(t_hi, c) < t_hi)));
1120 }
1121 
1122 // Let <x, x_off> = x_operands and <y, y_off> = y_operands.
1123 // If x == y and neither add(x, x_off) nor add(y, y_off) overflow, return
1124 // add(x, op(x_off, y_off)). Otherwise, return nullptr.
1125 Node* MaxNode::extract_add(PhaseGVN* phase, ConstAddOperands x_operands, ConstAddOperands y_operands) {
1126   Node* x = x_operands.first;
1127   Node* y = y_operands.first;
1128   int opcode = Opcode();
1129   assert(opcode == Op_MaxI || opcode == Op_MinI, "Unexpected opcode");
1130   const TypeInt* tx = phase->type(x)->isa_int();
1131   jint x_off = x_operands.second;
1132   jint y_off = y_operands.second;
1133   if (x == y && tx != nullptr &&
1134       !can_overflow(tx, x_off) &&
1135       !can_overflow(tx, y_off)) {
1136     jint c = opcode == Op_MinI ? MIN2(x_off, y_off) : MAX2(x_off, y_off);
1137     return new AddINode(x, phase->intcon(c));
1138   }
1139   return nullptr;
1140 }
1141 
1142 // Try to cast n as an integer addition with a constant. Return:
1143 //   <x, C>,       if n == add(x, C), where 'C' is a non-TOP constant;
1144 //   <nullptr, 0>, if n == add(x, C), where 'C' is a TOP constant; or
1145 //   <n, 0>,       otherwise.
1146 static ConstAddOperands as_add_with_constant(Node* n) {
1147   if (n->Opcode() != Op_AddI) {
1148     return ConstAddOperands(n, 0);
1149   }
1150   Node* x = n->in(1);
1151   Node* c = n->in(2);
1152   if (!c->is_Con()) {
1153     return ConstAddOperands(n, 0);
1154   }
1155   const Type* c_type = c->bottom_type();
1156   if (c_type == Type::TOP) {
1157     return ConstAddOperands(nullptr, 0);
1158   }
1159   return ConstAddOperands(x, c_type->is_int()->get_con());
1160 }
1161 
1162 Node* MaxNode::IdealI(PhaseGVN* phase, bool can_reshape) {
1163   int opcode = Opcode();
1164   assert(opcode == Op_MinI || opcode == Op_MaxI, "Unexpected opcode");
1165   // Try to transform the following pattern, in any of its four possible
1166   // permutations induced by op's commutativity:
1167   //     op(op(add(inner, inner_off), inner_other), add(outer, outer_off))
1168   // into
1169   //     op(add(inner, op(inner_off, outer_off)), inner_other),
1170   // where:
1171   //     op is either MinI or MaxI, and
1172   //     inner == outer, and
1173   //     the additions cannot overflow.
1174   for (uint inner_op_index = 1; inner_op_index <= 2; inner_op_index++) {
1175     if (in(inner_op_index)->Opcode() != opcode) {
1176       continue;
1177     }
1178     Node* outer_add = in(inner_op_index == 1 ? 2 : 1);
1179     ConstAddOperands outer_add_operands = as_add_with_constant(outer_add);
1180     if (outer_add_operands.first == nullptr) {
1181       return nullptr; // outer_add has a TOP input, no need to continue.
1182     }
1183     // One operand is a MinI/MaxI and the other is an integer addition with
1184     // constant. Test the operands of the inner MinI/MaxI.
1185     for (uint inner_add_index = 1; inner_add_index <= 2; inner_add_index++) {
1186       Node* inner_op = in(inner_op_index);
1187       Node* inner_add = inner_op->in(inner_add_index);
1188       ConstAddOperands inner_add_operands = as_add_with_constant(inner_add);
1189       if (inner_add_operands.first == nullptr) {
1190         return nullptr; // inner_add has a TOP input, no need to continue.
1191       }
1192       // Try to extract the inner add.
1193       Node* add_extracted = extract_add(phase, inner_add_operands, outer_add_operands);
1194       if (add_extracted == nullptr) {
1195         continue;
1196       }
1197       Node* add_transformed = phase->transform(add_extracted);
1198       Node* inner_other = inner_op->in(inner_add_index == 1 ? 2 : 1);
1199       return build_min_max_int(add_transformed, inner_other, opcode == Op_MaxI);
1200     }
1201   }
1202   // Try to transform
1203   //     op(add(x, x_off), add(y, y_off))
1204   // into
1205   //     add(x, op(x_off, y_off)),
1206   // where:
1207   //     op is either MinI or MaxI, and
1208   //     inner == outer, and
1209   //     the additions cannot overflow.
1210   ConstAddOperands xC = as_add_with_constant(in(1));
1211   ConstAddOperands yC = as_add_with_constant(in(2));
1212   if (xC.first == nullptr || yC.first == nullptr) return nullptr;
1213   return extract_add(phase, xC, yC);
1214 }
1215 
1216 // Ideal transformations for MaxINode
1217 Node* MaxINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1218   return IdealI(phase, can_reshape);
1219 }
1220 
1221 //=============================================================================
1222 //------------------------------add_ring---------------------------------------
1223 // Supplied function returns the sum of the inputs.
1224 const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
1225   const TypeInt *r0 = t0->is_int(); // Handy access
1226   const TypeInt *r1 = t1->is_int();
1227 
1228   // Otherwise just MAX them bits.
1229   return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1230 }
1231 
1232 //=============================================================================
1233 //------------------------------Idealize---------------------------------------
1234 // MINs show up in range-check loop limit calculations.  Look for
1235 // "MIN2(x+c0,MIN2(y,x+c1))".  Pick the smaller constant: "MIN2(x+c0,y)"
1236 Node* MinINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1237   return IdealI(phase, can_reshape);
1238 }
1239 
1240 //------------------------------add_ring---------------------------------------
1241 // Supplied function returns the sum of the inputs.
1242 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
1243   const TypeInt *r0 = t0->is_int(); // Handy access
1244   const TypeInt *r1 = t1->is_int();
1245 
1246   // Otherwise just MIN them bits.
1247   return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1248 }
1249 
1250 // Collapse the "addition with overflow-protection" pattern, and the symmetrical
1251 // "subtraction with underflow-protection" pattern. These are created during the
1252 // unrolling, when we have to adjust the limit by subtracting the stride, but want
1253 // to protect against underflow: MaxL(SubL(limit, stride), min_jint).
1254 // If we have more than one of those in a sequence:
1255 //
1256 //   x  con2
1257 //   |  |
1258 //   AddL  clamp2
1259 //     |    |
1260 //    Max/MinL con1
1261 //          |  |
1262 //          AddL  clamp1
1263 //            |    |
1264 //           Max/MinL (n)
1265 //
1266 // We want to collapse it to:
1267 //
1268 //   x  con1  con2
1269 //   |    |    |
1270 //   |   AddLNode (new_con)
1271 //   |    |
1272 //  AddLNode  clamp1
1273 //        |    |
1274 //       Max/MinL (n)
1275 //
1276 // Note: we assume that SubL was already replaced by an AddL, and that the stride
1277 // has its sign flipped: SubL(limit, stride) -> AddL(limit, -stride).
1278 Node* fold_subI_no_underflow_pattern(Node* n, PhaseGVN* phase) {
1279   assert(n->Opcode() == Op_MaxL || n->Opcode() == Op_MinL, "sanity");
1280   // Check that the two clamps have the correct values.
1281   jlong clamp = (n->Opcode() == Op_MaxL) ? min_jint : max_jint;
1282   auto is_clamp = [&](Node* c) {
1283     const TypeLong* t = phase->type(c)->isa_long();
1284     return t != nullptr && t->is_con() &&
1285            t->get_con() == clamp;
1286   };
1287   // Check that the constants are negative if MaxL, and positive if MinL.
1288   auto is_sub_con = [&](Node* c) {
1289     const TypeLong* t = phase->type(c)->isa_long();
1290     return t != nullptr && t->is_con() &&
1291            t->get_con() < max_jint && t->get_con() > min_jint &&
1292            (t->get_con() < 0) == (n->Opcode() == Op_MaxL);
1293   };
1294   // Verify the graph level by level:
1295   Node* add1   = n->in(1);
1296   Node* clamp1 = n->in(2);
1297   if (add1->Opcode() == Op_AddL && is_clamp(clamp1)) {
1298     Node* max2 = add1->in(1);
1299     Node* con1 = add1->in(2);
1300     if (max2->Opcode() == n->Opcode() && is_sub_con(con1)) {
1301       Node* add2   = max2->in(1);
1302       Node* clamp2 = max2->in(2);
1303       if (add2->Opcode() == Op_AddL && is_clamp(clamp2)) {
1304         Node* x    = add2->in(1);
1305         Node* con2 = add2->in(2);
1306         if (is_sub_con(con2)) {
1307           Node* new_con = phase->transform(new AddLNode(con1, con2));
1308           Node* new_sub = phase->transform(new AddLNode(x, new_con));
1309           n->set_req_X(1, new_sub, phase);
1310           return n;
1311         }
1312       }
1313     }
1314   }
1315   return nullptr;
1316 }
1317 
1318 const Type* MaxLNode::add_ring(const Type* t0, const Type* t1) const {
1319   const TypeLong* r0 = t0->is_long();
1320   const TypeLong* r1 = t1->is_long();
1321 
1322   return TypeLong::make(MAX2(r0->_lo, r1->_lo), MAX2(r0->_hi, r1->_hi), MAX2(r0->_widen, r1->_widen));
1323 }
1324 
1325 Node* MaxLNode::Identity(PhaseGVN* phase) {
1326   const TypeLong* t1 = phase->type(in(1))->is_long();
1327   const TypeLong* t2 = phase->type(in(2))->is_long();
1328 
1329   // Can we determine maximum statically?
1330   if (t1->_lo >= t2->_hi) {
1331     return in(1);
1332   } else if (t2->_lo >= t1->_hi) {
1333     return in(2);
1334   }
1335 
1336   return MaxNode::Identity(phase);
1337 }
1338 
1339 Node* MaxLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1340   Node* n = AddNode::Ideal(phase, can_reshape);
1341   if (n != nullptr) {
1342     return n;
1343   }
1344   if (can_reshape) {
1345     return fold_subI_no_underflow_pattern(this, phase);
1346   }
1347   return nullptr;
1348 }
1349 
1350 const Type* MinLNode::add_ring(const Type* t0, const Type* t1) const {
1351   const TypeLong* r0 = t0->is_long();
1352   const TypeLong* r1 = t1->is_long();
1353 
1354   return TypeLong::make(MIN2(r0->_lo, r1->_lo), MIN2(r0->_hi, r1->_hi), MIN2(r0->_widen, r1->_widen));
1355 }
1356 
1357 Node* MinLNode::Identity(PhaseGVN* phase) {
1358   const TypeLong* t1 = phase->type(in(1))->is_long();
1359   const TypeLong* t2 = phase->type(in(2))->is_long();
1360 
1361   // Can we determine minimum statically?
1362   if (t1->_lo >= t2->_hi) {
1363     return in(2);
1364   } else if (t2->_lo >= t1->_hi) {
1365     return in(1);
1366   }
1367 
1368   return MaxNode::Identity(phase);
1369 }
1370 
1371 Node* MinLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1372   Node* n = AddNode::Ideal(phase, can_reshape);
1373   if (n != nullptr) {
1374     return n;
1375   }
1376   if (can_reshape) {
1377     return fold_subI_no_underflow_pattern(this, phase);
1378   }
1379   return nullptr;
1380 }
1381 
1382 //------------------------------add_ring---------------------------------------
1383 const Type* MinFNode::add_ring(const Type* t0, const Type* t1 ) const {
1384   const TypeF* r0 = t0->isa_float_constant();
1385   const TypeF* r1 = t1->isa_float_constant();
1386   if (r0 == nullptr || r1 == nullptr) {
1387     return bottom_type();
1388   }
1389 
1390   if (r0->is_nan()) {
1391     return r0;
1392   }
1393   if (r1->is_nan()) {
1394     return r1;
1395   }
1396 
1397   float f0 = r0->getf();
1398   float f1 = r1->getf();
1399   if (f0 != 0.0f || f1 != 0.0f) {
1400     return f0 < f1 ? r0 : r1;
1401   }
1402 
1403   // handle min of 0.0, -0.0 case.
1404   return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1;
1405 }
1406 
1407 //------------------------------add_ring---------------------------------------
1408 const Type* MinDNode::add_ring(const Type* t0, const Type* t1) const {
1409   const TypeD* r0 = t0->isa_double_constant();
1410   const TypeD* r1 = t1->isa_double_constant();
1411   if (r0 == nullptr || r1 == nullptr) {
1412     return bottom_type();
1413   }
1414 
1415   if (r0->is_nan()) {
1416     return r0;
1417   }
1418   if (r1->is_nan()) {
1419     return r1;
1420   }
1421 
1422   double d0 = r0->getd();
1423   double d1 = r1->getd();
1424   if (d0 != 0.0 || d1 != 0.0) {
1425     return d0 < d1 ? r0 : r1;
1426   }
1427 
1428   // handle min of 0.0, -0.0 case.
1429   return (jlong_cast(d0) < jlong_cast(d1)) ? r0 : r1;
1430 }
1431 
1432 //------------------------------add_ring---------------------------------------
1433 const Type* MaxFNode::add_ring(const Type* t0, const Type* t1) const {
1434   const TypeF* r0 = t0->isa_float_constant();
1435   const TypeF* r1 = t1->isa_float_constant();
1436   if (r0 == nullptr || r1 == nullptr) {
1437     return bottom_type();
1438   }
1439 
1440   if (r0->is_nan()) {
1441     return r0;
1442   }
1443   if (r1->is_nan()) {
1444     return r1;
1445   }
1446 
1447   float f0 = r0->getf();
1448   float f1 = r1->getf();
1449   if (f0 != 0.0f || f1 != 0.0f) {
1450     return f0 > f1 ? r0 : r1;
1451   }
1452 
1453   // handle max of 0.0,-0.0 case.
1454   return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1;
1455 }
1456 
1457 //------------------------------add_ring---------------------------------------
1458 const Type* MaxDNode::add_ring(const Type* t0, const Type* t1) const {
1459   const TypeD* r0 = t0->isa_double_constant();
1460   const TypeD* r1 = t1->isa_double_constant();
1461   if (r0 == nullptr || r1 == nullptr) {
1462     return bottom_type();
1463   }
1464 
1465   if (r0->is_nan()) {
1466     return r0;
1467   }
1468   if (r1->is_nan()) {
1469     return r1;
1470   }
1471 
1472   double d0 = r0->getd();
1473   double d1 = r1->getd();
1474   if (d0 != 0.0 || d1 != 0.0) {
1475     return d0 > d1 ? r0 : r1;
1476   }
1477 
1478   // handle max of 0.0, -0.0 case.
1479   return (jlong_cast(d0) > jlong_cast(d1)) ? r0 : r1;
1480 }