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   // Convert a + a + ... + a into a*n
 399   Node* serial_additions = convert_serial_additions(phase, bt);
 400   if (serial_additions != nullptr) {
 401     return serial_additions;
 402   }
 403 
 404   return AddNode::Ideal(phase, can_reshape);
 405 }
 406 
 407 // Try to convert a serial of additions into a single multiplication. Also convert `(a * CON) + a` to `(CON + 1) * a` as
 408 // a side effect. On success, a new MulNode is returned.
 409 Node* AddNode::convert_serial_additions(PhaseGVN* phase, BasicType bt) {
 410   // We need to make sure that the current AddNode is not part of a MulNode that has already been optimized to a
 411   // power-of-2 addition (e.g., 3 * a => (a << 2) + a). Without this check, GVN would keep trying to optimize the same
 412   // node and can't progress. For example, 3 * a => (a << 2) + a => 3 * a => (a << 2) + a => ...
 413   if (find_power_of_two_addition_pattern(this, bt, nullptr) != nullptr) {
 414     return nullptr;
 415   }
 416 
 417   Node* in1 = in(1);
 418   Node* in2 = in(2);
 419   jlong multiplier;
 420 
 421   // While multiplications can be potentially optimized to power-of-2 subtractions (e.g., a * 7 => (a << 3) - a),
 422   // (x - y) + y => x is already handled by the Identity() methods. So, we don't need to check for that pattern here.
 423   if (find_simple_addition_pattern(in1, bt, &multiplier) == in2
 424       || find_simple_lshift_pattern(in1, bt, &multiplier) == in2
 425       || find_simple_multiplication_pattern(in1, bt, &multiplier) == in2
 426       || find_power_of_two_addition_pattern(in1, bt, &multiplier) == in2) {
 427     multiplier++; // +1 for the in2 term
 428 
 429     Node* con = (bt == T_INT)
 430                 ? (Node*) phase->intcon((jint) multiplier) // intentional type narrowing to allow overflow at max_jint
 431                 : (Node*) phase->longcon(multiplier);
 432     return MulNode::make(con, in2, bt);
 433   }
 434 
 435   return nullptr;
 436 }
 437 
 438 // Try to match `a + a`. On success, return `a` and set `2` as `multiplier`.
 439 // The method matches `n` for pattern: AddNode(a, a).
 440 Node* AddNode::find_simple_addition_pattern(Node* n, BasicType bt, jlong* multiplier) {
 441   if (n->Opcode() == Op_Add(bt) && n->in(1) == n->in(2)) {
 442     *multiplier = 2;
 443     return n->in(1);
 444   }
 445 
 446   return nullptr;
 447 }
 448 
 449 // Try to match `a << CON`. On success, return `a` and set `1 << CON` as `multiplier`.
 450 // Match `n` for pattern: LShiftNode(a, CON).
 451 // Note that the power-of-2 multiplication optimization could potentially convert a MulNode to this pattern.
 452 Node* AddNode::find_simple_lshift_pattern(Node* n, BasicType bt, jlong* multiplier) {
 453   // Note that power-of-2 multiplication optimization could potentially convert a MulNode to this pattern
 454   if (n->Opcode() == Op_LShift(bt) && n->in(2)->is_Con()) {
 455     Node* con = n->in(2);
 456     if (con->is_top()) {
 457       return nullptr;
 458     }
 459 
 460     *multiplier = ((jlong) 1 << con->get_int());
 461     return n->in(1);
 462   }
 463 
 464   return nullptr;
 465 }
 466 
 467 // Try to match `CON * a`. On success, return `a` and set `CON` as `multiplier`.
 468 // Match `n` for patterns:
 469 //     - MulNode(CON, a)
 470 //     - MulNode(a, CON)
 471 Node* AddNode::find_simple_multiplication_pattern(Node* n, BasicType bt, jlong* multiplier) {
 472   // This optimization technically only produces MulNode(CON, a), but we might as match MulNode(a, CON), too.
 473   if (n->Opcode() == Op_Mul(bt) && (n->in(1)->is_Con() || n->in(2)->is_Con())) {
 474     Node* con = n->in(1);
 475     Node* base = n->in(2);
 476 
 477     // swap ConNode to lhs for easier matching
 478     if (!con->is_Con()) {
 479       swap(con, base);
 480     }
 481 
 482     if (con->is_top()) {
 483       return nullptr;
 484     }
 485 
 486     *multiplier = con->get_integer_as_long(bt);
 487     return base;
 488   }
 489 
 490   return nullptr;
 491 }
 492 
 493 // Try to match `(a << CON1) + (a << CON2)`. On success, return `a` and set `(1 << CON1) + (1 << CON2)` as `multiplier`.
 494 // Match `n` for patterns:
 495 //     - AddNode(LShiftNode(a, CON), LShiftNode(a, CON)/a)
 496 //     - AddNode(LShiftNode(a, CON)/a, LShiftNode(a, CON))
 497 // given that lhs is different from rhs.
 498 // Note that one of the term of the addition could simply be `a` (i.e., a << 0). Calling this function with `multiplier`
 499 // being null is safe.
 500 Node* AddNode::find_power_of_two_addition_pattern(Node* n, BasicType bt, jlong* multiplier) {
 501   if (n->Opcode() == Op_Add(bt) && n->in(1) != n->in(2)) {
 502     Node* lhs = n->in(1);
 503     Node* rhs = n->in(2);
 504 
 505     // swap LShiftNode to lhs for easier matching
 506     if (lhs->Opcode() != Op_LShift(bt)) {
 507       swap(lhs, rhs);
 508     }
 509 
 510     // AddNode(LShiftNode(a, CON), *)?
 511     if (lhs->Opcode() != Op_LShift(bt) || !lhs->in(2)->is_Con()) {
 512       return nullptr;
 513     }
 514 
 515     jlong lhs_multiplier = 0;
 516     if (multiplier != nullptr) {
 517       Node* con = lhs->in(2);
 518       if (con->is_top()) {
 519         return nullptr;
 520       }
 521 
 522       lhs_multiplier = (jlong) 1 << con->get_int();
 523     }
 524 
 525     // AddNode(LShiftNode(a, CON), a)?
 526     if (lhs->in(1) == rhs) {
 527       if (multiplier != nullptr) {
 528         *multiplier = lhs_multiplier + 1;
 529       }
 530 
 531       return rhs;
 532     }
 533 
 534     // AddNode(LShiftNode(a, CON), LShiftNode(a, CON2))?
 535     if (rhs->Opcode() == Op_LShift(bt) && lhs->in(1) == rhs->in(1) && rhs->in(2)->is_Con()) {
 536       if (multiplier != nullptr) {
 537         Node* con = rhs->in(2);
 538         if (con->is_top()) {
 539           return nullptr;
 540         }
 541 
 542         *multiplier = lhs_multiplier + ((jlong) 1 << con->get_int());
 543       }
 544 
 545       return lhs->in(1);
 546     }
 547     return nullptr;
 548   }
 549   return nullptr;
 550 }
 551 
 552 Node* AddINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 553   Node* in1 = in(1);
 554   Node* in2 = in(2);
 555   int op1 = in1->Opcode();
 556   int op2 = in2->Opcode();
 557 
 558   // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
 559   // Helps with array allocation math constant folding
 560   // See 4790063:
 561   // Unrestricted transformation is unsafe for some runtime values of 'x'
 562   // ( x ==  0, z == 1, y == -1 ) fails
 563   // ( x == -5, z == 1, y ==  1 ) fails
 564   // Transform works for small z and small negative y when the addition
 565   // (x + (y << z)) does not cross zero.
 566   // Implement support for negative y and (x >= -(y << z))
 567   // Have not observed cases where type information exists to support
 568   // positive y and (x <= -(y << z))
 569   if (op1 == Op_URShiftI && op2 == Op_ConI &&
 570       in1->in(2)->Opcode() == Op_ConI) {
 571     jint z = phase->type(in1->in(2))->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
 572     jint y = phase->type(in2)->is_int()->get_con();
 573 
 574     if (z < 5 && -5 < y && y < 0) {
 575       const Type* t_in11 = phase->type(in1->in(1));
 576       if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z))) {
 577         Node* a = phase->transform(new AddINode( in1->in(1), phase->intcon(y<<z)));
 578         return new URShiftINode(a, in1->in(2));
 579       }
 580     }
 581   }
 582 
 583   return AddNode::IdealIL(phase, can_reshape, T_INT);
 584 }
 585 
 586 
 587 //------------------------------Identity---------------------------------------
 588 // Fold (x-y)+y  OR  y+(x-y)  into  x
 589 Node* AddINode::Identity(PhaseGVN* phase) {
 590   if (in(1)->Opcode() == Op_SubI && in(1)->in(2) == in(2)) {
 591     return in(1)->in(1);
 592   } else if (in(2)->Opcode() == Op_SubI && in(2)->in(2) == in(1)) {
 593     return in(2)->in(1);
 594   }
 595   return AddNode::Identity(phase);
 596 }
 597 
 598 
 599 //------------------------------add_ring---------------------------------------
 600 // Supplied function returns the sum of the inputs.  Guaranteed never
 601 // to be passed a TOP or BOTTOM type, these are filtered out by
 602 // pre-check.
 603 const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {
 604   const TypeInt *r0 = t0->is_int(); // Handy access
 605   const TypeInt *r1 = t1->is_int();
 606   int lo = java_add(r0->_lo, r1->_lo);
 607   int hi = java_add(r0->_hi, r1->_hi);
 608   if( !(r0->is_con() && r1->is_con()) ) {
 609     // Not both constants, compute approximate result
 610     if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
 611       lo = min_jint; hi = max_jint; // Underflow on the low side
 612     }
 613     if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
 614       lo = min_jint; hi = max_jint; // Overflow on the high side
 615     }
 616     if( lo > hi ) {               // Handle overflow
 617       lo = min_jint; hi = max_jint;
 618     }
 619   } else {
 620     // both constants, compute precise result using 'lo' and 'hi'
 621     // Semantics define overflow and underflow for integer addition
 622     // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
 623   }
 624   return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
 625 }
 626 
 627 
 628 //=============================================================================
 629 //------------------------------Idealize---------------------------------------
 630 Node* AddLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
 631   return AddNode::IdealIL(phase, can_reshape, T_LONG);
 632 }
 633 
 634 
 635 //------------------------------Identity---------------------------------------
 636 // Fold (x-y)+y  OR  y+(x-y)  into  x
 637 Node* AddLNode::Identity(PhaseGVN* phase) {
 638   if (in(1)->Opcode() == Op_SubL && in(1)->in(2) == in(2)) {
 639     return in(1)->in(1);
 640   } else if (in(2)->Opcode() == Op_SubL && in(2)->in(2) == in(1)) {
 641     return in(2)->in(1);
 642   }
 643   return AddNode::Identity(phase);
 644 }
 645 
 646 
 647 //------------------------------add_ring---------------------------------------
 648 // Supplied function returns the sum of the inputs.  Guaranteed never
 649 // to be passed a TOP or BOTTOM type, these are filtered out by
 650 // pre-check.
 651 const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {
 652   const TypeLong *r0 = t0->is_long(); // Handy access
 653   const TypeLong *r1 = t1->is_long();
 654   jlong lo = java_add(r0->_lo, r1->_lo);
 655   jlong hi = java_add(r0->_hi, r1->_hi);
 656   if( !(r0->is_con() && r1->is_con()) ) {
 657     // Not both constants, compute approximate result
 658     if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
 659       lo =min_jlong; hi = max_jlong; // Underflow on the low side
 660     }
 661     if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
 662       lo = min_jlong; hi = max_jlong; // Overflow on the high side
 663     }
 664     if( lo > hi ) {               // Handle overflow
 665       lo = min_jlong; hi = max_jlong;
 666     }
 667   } else {
 668     // both constants, compute precise result using 'lo' and 'hi'
 669     // Semantics define overflow and underflow for integer addition
 670     // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
 671   }
 672   return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
 673 }
 674 
 675 
 676 //=============================================================================
 677 //------------------------------add_of_identity--------------------------------
 678 // Check for addition of the identity
 679 const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {
 680   // x ADD 0  should return x unless 'x' is a -zero
 681   //
 682   // const Type *zero = add_id();     // The additive identity
 683   // jfloat f1 = t1->getf();
 684   // jfloat f2 = t2->getf();
 685   //
 686   // if( t1->higher_equal( zero ) ) return t2;
 687   // if( t2->higher_equal( zero ) ) return t1;
 688 
 689   return nullptr;
 690 }
 691 
 692 //------------------------------add_ring---------------------------------------
 693 // Supplied function returns the sum of the inputs.
 694 // This also type-checks the inputs for sanity.  Guaranteed never to
 695 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 696 const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {
 697   if (!t0->isa_float_constant() || !t1->isa_float_constant()) {
 698     return bottom_type();
 699   }
 700   return TypeF::make( t0->getf() + t1->getf() );
 701 }
 702 
 703 //------------------------------Ideal------------------------------------------
 704 Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 705   // Floating point additions are not associative because of boundary conditions (infinity)
 706   return commute(phase, this) ? this : nullptr;
 707 }
 708 
 709 
 710 //=============================================================================
 711 //------------------------------add_of_identity--------------------------------
 712 // Check for addition of the identity
 713 const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {
 714   // x ADD 0  should return x unless 'x' is a -zero
 715   //
 716   // const Type *zero = add_id();     // The additive identity
 717   // jfloat f1 = t1->getf();
 718   // jfloat f2 = t2->getf();
 719   //
 720   // if( t1->higher_equal( zero ) ) return t2;
 721   // if( t2->higher_equal( zero ) ) return t1;
 722 
 723   return nullptr;
 724 }
 725 //------------------------------add_ring---------------------------------------
 726 // Supplied function returns the sum of the inputs.
 727 // This also type-checks the inputs for sanity.  Guaranteed never to
 728 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 729 const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {
 730   if (!t0->isa_double_constant() || !t1->isa_double_constant()) {
 731     return bottom_type();
 732   }
 733   return TypeD::make( t0->getd() + t1->getd() );
 734 }
 735 
 736 //------------------------------Ideal------------------------------------------
 737 Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 738   // Floating point additions are not associative because of boundary conditions (infinity)
 739   return commute(phase, this) ? this : nullptr;
 740 }
 741 
 742 
 743 //=============================================================================
 744 //------------------------------Identity---------------------------------------
 745 // If one input is a constant 0, return the other input.
 746 Node* AddPNode::Identity(PhaseGVN* phase) {
 747   return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;
 748 }
 749 
 750 //------------------------------Idealize---------------------------------------
 751 Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 752   // Bail out if dead inputs
 753   if( phase->type( in(Address) ) == Type::TOP ) return nullptr;
 754 
 755   // If the left input is an add of a constant, flatten the expression tree.
 756   const Node *n = in(Address);
 757   if (n->is_AddP() && n->in(Base) == in(Base)) {
 758     const AddPNode *addp = n->as_AddP(); // Left input is an AddP
 759     assert( !addp->in(Address)->is_AddP() ||
 760              addp->in(Address)->as_AddP() != addp,
 761             "dead loop in AddPNode::Ideal" );
 762     // Type of left input's right input
 763     const Type *t = phase->type( addp->in(Offset) );
 764     if( t == Type::TOP ) return nullptr;
 765     const TypeX *t12 = t->is_intptr_t();
 766     if( t12->is_con() ) {       // Left input is an add of a constant?
 767       // If the right input is a constant, combine constants
 768       const Type *temp_t2 = phase->type( in(Offset) );
 769       if( temp_t2 == Type::TOP ) return nullptr;
 770       const TypeX *t2 = temp_t2->is_intptr_t();
 771       Node* address;
 772       Node* offset;
 773       if( t2->is_con() ) {
 774         // The Add of the flattened expression
 775         address = addp->in(Address);
 776         offset  = phase->MakeConX(t2->get_con() + t12->get_con());
 777       } else {
 778         // Else move the constant to the right.  ((A+con)+B) into ((A+B)+con)
 779         address = phase->transform(new AddPNode(in(Base),addp->in(Address),in(Offset)));
 780         offset  = addp->in(Offset);
 781       }
 782       set_req_X(Address, address, phase);
 783       set_req_X(Offset, offset, phase);
 784       return this;
 785     }
 786   }
 787 
 788   // Raw pointers?
 789   if( in(Base)->bottom_type() == Type::TOP ) {
 790     // If this is a null+long form (from unsafe accesses), switch to a rawptr.
 791     if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
 792       Node* offset = in(Offset);
 793       return new CastX2PNode(offset);
 794     }
 795   }
 796 
 797   // If the right is an add of a constant, push the offset down.
 798   // Convert: (ptr + (offset+con)) into (ptr+offset)+con.
 799   // The idea is to merge array_base+scaled_index groups together,
 800   // and only have different constant offsets from the same base.
 801   const Node *add = in(Offset);
 802   if( add->Opcode() == Op_AddX && add->in(1) != add ) {
 803     const Type *t22 = phase->type( add->in(2) );
 804     if( t22->singleton() && (t22 != Type::TOP) ) {  // Right input is an add of a constant?
 805       set_req(Address, phase->transform(new AddPNode(in(Base),in(Address),add->in(1))));
 806       set_req_X(Offset, add->in(2), phase); // puts add on igvn worklist if needed
 807       return this;              // Made progress
 808     }
 809   }
 810 
 811   return nullptr;                  // No progress
 812 }
 813 
 814 //------------------------------bottom_type------------------------------------
 815 // Bottom-type is the pointer-type with unknown offset.
 816 const Type *AddPNode::bottom_type() const {
 817   if (in(Address) == nullptr)  return TypePtr::BOTTOM;
 818   const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
 819   if( !tp ) return Type::TOP;   // TOP input means TOP output
 820   assert( in(Offset)->Opcode() != Op_ConP, "" );
 821   const Type *t = in(Offset)->bottom_type();
 822   if( t == Type::TOP )
 823     return tp->add_offset(Type::OffsetTop);
 824   const TypeX *tx = t->is_intptr_t();
 825   intptr_t txoffset = Type::OffsetBot;
 826   if (tx->is_con()) {   // Left input is an add of a constant?
 827     txoffset = tx->get_con();
 828   }
 829   if (tp->isa_aryptr()) {
 830     // In the case of a flat inline type array, each field has its
 831     // own slice so we need to extract the field being accessed from
 832     // the address computation
 833     return tp->is_aryptr()->add_field_offset_and_offset(txoffset);
 834   }
 835   return tp->add_offset(txoffset);
 836 }
 837 
 838 //------------------------------Value------------------------------------------
 839 const Type* AddPNode::Value(PhaseGVN* phase) const {
 840   // Either input is TOP ==> the result is TOP
 841   const Type *t1 = phase->type( in(Address) );
 842   const Type *t2 = phase->type( in(Offset) );
 843   if( t1 == Type::TOP ) return Type::TOP;
 844   if( t2 == Type::TOP ) return Type::TOP;
 845 
 846   // Left input is a pointer
 847   const TypePtr *p1 = t1->isa_ptr();
 848   // Right input is an int
 849   const TypeX *p2 = t2->is_intptr_t();
 850   // Add 'em
 851   intptr_t p2offset = Type::OffsetBot;
 852   if (p2->is_con()) {   // Left input is an add of a constant?
 853     p2offset = p2->get_con();
 854   }
 855   if (p1->isa_aryptr()) {
 856     // In the case of a flat inline type array, each field has its
 857     // own slice so we need to extract the field being accessed from
 858     // the address computation
 859     return p1->is_aryptr()->add_field_offset_and_offset(p2offset);
 860   }
 861   return p1->add_offset(p2offset);
 862 }
 863 
 864 //------------------------Ideal_base_and_offset--------------------------------
 865 // Split an oop pointer into a base and offset.
 866 // (The offset might be Type::OffsetBot in the case of an array.)
 867 // Return the base, or null if failure.
 868 Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseValues* phase,
 869                                       // second return value:
 870                                       intptr_t& offset) {
 871   if (ptr->is_AddP()) {
 872     Node* base = ptr->in(AddPNode::Base);
 873     Node* addr = ptr->in(AddPNode::Address);
 874     Node* offs = ptr->in(AddPNode::Offset);
 875     if (base == addr || base->is_top()) {
 876       offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
 877       if (offset != Type::OffsetBot) {
 878         return addr;
 879       }
 880     }
 881   }
 882   offset = Type::OffsetBot;
 883   return nullptr;
 884 }
 885 
 886 //------------------------------unpack_offsets----------------------------------
 887 // Collect the AddP offset values into the elements array, giving up
 888 // if there are more than length.
 889 int AddPNode::unpack_offsets(Node* elements[], int length) const {
 890   int count = 0;
 891   Node const* addr = this;
 892   Node* base = addr->in(AddPNode::Base);
 893   while (addr->is_AddP()) {
 894     if (addr->in(AddPNode::Base) != base) {
 895       // give up
 896       return -1;
 897     }
 898     elements[count++] = addr->in(AddPNode::Offset);
 899     if (count == length) {
 900       // give up
 901       return -1;
 902     }
 903     addr = addr->in(AddPNode::Address);
 904   }
 905   if (addr != base) {
 906     return -1;
 907   }
 908   return count;
 909 }
 910 
 911 //------------------------------match_edge-------------------------------------
 912 // Do we Match on this edge index or not?  Do not match base pointer edge
 913 uint AddPNode::match_edge(uint idx) const {
 914   return idx > Base;
 915 }
 916 
 917 //=============================================================================
 918 //------------------------------Identity---------------------------------------
 919 Node* OrINode::Identity(PhaseGVN* phase) {
 920   // x | x => x
 921   if (in(1) == in(2)) {
 922     return in(1);
 923   }
 924 
 925   return AddNode::Identity(phase);
 926 }
 927 
 928 // Find shift value for Integer or Long OR.
 929 static Node* rotate_shift(PhaseGVN* phase, Node* lshift, Node* rshift, int mask) {
 930   // val << norm_con_shift | val >> ({32|64} - norm_con_shift) => rotate_left val, norm_con_shift
 931   const TypeInt* lshift_t = phase->type(lshift)->isa_int();
 932   const TypeInt* rshift_t = phase->type(rshift)->isa_int();
 933   if (lshift_t != nullptr && lshift_t->is_con() &&
 934       rshift_t != nullptr && rshift_t->is_con() &&
 935       ((lshift_t->get_con() & mask) == ((mask + 1) - (rshift_t->get_con() & mask)))) {
 936     return phase->intcon(lshift_t->get_con() & mask);
 937   }
 938   // val << var_shift | val >> ({0|32|64} - var_shift) => rotate_left val, var_shift
 939   if (rshift->Opcode() == Op_SubI && rshift->in(2) == lshift && rshift->in(1)->is_Con()){
 940     const TypeInt* shift_t = phase->type(rshift->in(1))->isa_int();
 941     if (shift_t != nullptr && shift_t->is_con() &&
 942         (shift_t->get_con() == 0 || shift_t->get_con() == (mask + 1))) {
 943       return lshift;
 944     }
 945   }
 946   return nullptr;
 947 }
 948 
 949 Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) {
 950   int lopcode = in(1)->Opcode();
 951   int ropcode = in(2)->Opcode();
 952   if (Matcher::match_rule_supported(Op_RotateLeft) &&
 953       lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) {
 954     Node* lshift = in(1)->in(2);
 955     Node* rshift = in(2)->in(2);
 956     Node* shift = rotate_shift(phase, lshift, rshift, 0x1F);
 957     if (shift != nullptr) {
 958       return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT);
 959     }
 960     return nullptr;
 961   }
 962   if (Matcher::match_rule_supported(Op_RotateRight) &&
 963       lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) {
 964     Node* rshift = in(1)->in(2);
 965     Node* lshift = in(2)->in(2);
 966     Node* shift = rotate_shift(phase, rshift, lshift, 0x1F);
 967     if (shift != nullptr) {
 968       return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
 969     }
 970   }
 971 
 972   // Convert "~a | ~b" into "~(a & b)"
 973   if (AddNode::is_not(phase, in(1), T_INT) && AddNode::is_not(phase, in(2), T_INT)) {
 974     Node* and_a_b = new AndINode(in(1)->in(1), in(2)->in(1));
 975     Node* tn = phase->transform(and_a_b);
 976     return AddNode::make_not(phase, tn, T_INT);
 977   }
 978   return nullptr;
 979 }
 980 
 981 //------------------------------add_ring---------------------------------------
 982 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
 983 // the logical operations the ring's ADD is really a logical OR function.
 984 // This also type-checks the inputs for sanity.  Guaranteed never to
 985 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 986 const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
 987   const TypeInt *r0 = t0->is_int(); // Handy access
 988   const TypeInt *r1 = t1->is_int();
 989 
 990   // If both args are bool, can figure out better types
 991   if ( r0 == TypeInt::BOOL ) {
 992     if ( r1 == TypeInt::ONE) {
 993       return TypeInt::ONE;
 994     } else if ( r1 == TypeInt::BOOL ) {
 995       return TypeInt::BOOL;
 996     }
 997   } else if ( r0 == TypeInt::ONE ) {
 998     if ( r1 == TypeInt::BOOL ) {
 999       return TypeInt::ONE;
1000     }
1001   }
1002 
1003   // If either input is not a constant, just return all integers.
1004   if( !r0->is_con() || !r1->is_con() )
1005     return TypeInt::INT;        // Any integer, but still no symbols.
1006 
1007   // Otherwise just OR them bits.
1008   return TypeInt::make( r0->get_con() | r1->get_con() );
1009 }
1010 
1011 //=============================================================================
1012 //------------------------------Identity---------------------------------------
1013 Node* OrLNode::Identity(PhaseGVN* phase) {
1014   // x | x => x
1015   if (in(1) == in(2)) {
1016     return in(1);
1017   }
1018 
1019   return AddNode::Identity(phase);
1020 }
1021 
1022 Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1023   int lopcode = in(1)->Opcode();
1024   int ropcode = in(2)->Opcode();
1025   if (Matcher::match_rule_supported(Op_RotateLeft) &&
1026       lopcode == Op_LShiftL && ropcode == Op_URShiftL && in(1)->in(1) == in(2)->in(1)) {
1027     Node* lshift = in(1)->in(2);
1028     Node* rshift = in(2)->in(2);
1029     Node* shift = rotate_shift(phase, lshift, rshift, 0x3F);
1030     if (shift != nullptr) {
1031       return new RotateLeftNode(in(1)->in(1), shift, TypeLong::LONG);
1032     }
1033     return nullptr;
1034   }
1035   if (Matcher::match_rule_supported(Op_RotateRight) &&
1036       lopcode == Op_URShiftL && ropcode == Op_LShiftL && in(1)->in(1) == in(2)->in(1)) {
1037     Node* rshift = in(1)->in(2);
1038     Node* lshift = in(2)->in(2);
1039     Node* shift = rotate_shift(phase, rshift, lshift, 0x3F);
1040     if (shift != nullptr) {
1041       return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG);
1042     }
1043   }
1044 
1045   // Convert "~a | ~b" into "~(a & b)"
1046   if (AddNode::is_not(phase, in(1), T_LONG) && AddNode::is_not(phase, in(2), T_LONG)) {
1047     Node* and_a_b = new AndLNode(in(1)->in(1), in(2)->in(1));
1048     Node* tn = phase->transform(and_a_b);
1049     return AddNode::make_not(phase, tn, T_LONG);
1050   }
1051 
1052   return nullptr;
1053 }
1054 
1055 //------------------------------add_ring---------------------------------------
1056 const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
1057   const TypeLong *r0 = t0->is_long(); // Handy access
1058   const TypeLong *r1 = t1->is_long();
1059 
1060   // If either input is not a constant, just return all integers.
1061   if( !r0->is_con() || !r1->is_con() )
1062     return TypeLong::LONG;      // Any integer, but still no symbols.
1063 
1064   // Otherwise just OR them bits.
1065   return TypeLong::make( r0->get_con() | r1->get_con() );
1066 }
1067 
1068 //---------------------------Helper -------------------------------------------
1069 /* Decide if the given node is used only in arithmetic expressions(add or sub).
1070  */
1071 static bool is_used_in_only_arithmetic(Node* n, BasicType bt) {
1072   for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1073     Node* u = n->fast_out(i);
1074     if (u->Opcode() != Op_Add(bt) && u->Opcode() != Op_Sub(bt)) {
1075       return false;
1076     }
1077   }
1078   return true;
1079 }
1080 
1081 //=============================================================================
1082 //------------------------------Idealize---------------------------------------
1083 Node* XorINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1084   Node* in1 = in(1);
1085   Node* in2 = in(2);
1086 
1087   // Convert ~x into -1-x when ~x is used in an arithmetic expression
1088   // or x itself is an expression.
1089   if (phase->type(in2) == TypeInt::MINUS_1) { // follows LHS^(-1), i.e., ~LHS
1090     if (phase->is_IterGVN()) {
1091       if (is_used_in_only_arithmetic(this, T_INT)
1092           // LHS is arithmetic
1093           || (in1->Opcode() == Op_AddI || in1->Opcode() == Op_SubI)) {
1094         return new SubINode(in2, in1);
1095       }
1096     } else {
1097       // graph could be incomplete in GVN so we postpone to IGVN
1098       phase->record_for_igvn(this);
1099     }
1100   }
1101 
1102   // Propagate xor through constant cmoves. This pattern can occur after expansion of Conv2B nodes.
1103   const TypeInt* in2_type = phase->type(in2)->isa_int();
1104   if (in1->Opcode() == Op_CMoveI && in2_type != nullptr && in2_type->is_con()) {
1105     int in2_val = in2_type->get_con();
1106 
1107     // Get types of both sides of the CMove
1108     const TypeInt* left = phase->type(in1->in(CMoveNode::IfFalse))->isa_int();
1109     const TypeInt* right = phase->type(in1->in(CMoveNode::IfTrue))->isa_int();
1110 
1111     // Ensure that both sides are int constants
1112     if (left != nullptr && right != nullptr && left->is_con() && right->is_con()) {
1113       Node* cond = in1->in(CMoveNode::Condition);
1114 
1115       // Check that the comparison is a bool and that the cmp node type is correct
1116       if (cond->is_Bool()) {
1117         int cmp_op = cond->in(1)->Opcode();
1118 
1119         if (cmp_op == Op_CmpI || cmp_op == Op_CmpP) {
1120           int l_val = left->get_con();
1121           int r_val = right->get_con();
1122 
1123           return new CMoveINode(cond, phase->intcon(l_val ^ in2_val), phase->intcon(r_val ^ in2_val), TypeInt::INT);
1124         }
1125       }
1126     }
1127   }
1128 
1129   return AddNode::Ideal(phase, can_reshape);
1130 }
1131 
1132 const Type* XorINode::Value(PhaseGVN* phase) const {
1133   Node* in1 = in(1);
1134   Node* in2 = in(2);
1135   const Type* t1 = phase->type(in1);
1136   const Type* t2 = phase->type(in2);
1137   if (t1 == Type::TOP || t2 == Type::TOP) {
1138     return Type::TOP;
1139   }
1140   // x ^ x ==> 0
1141   if (in1->eqv_uncast(in2)) {
1142     return add_id();
1143   }
1144   // result of xor can only have bits sets where any of the
1145   // inputs have bits set. lo can always become 0.
1146   const TypeInt* t1i = t1->is_int();
1147   const TypeInt* t2i = t2->is_int();
1148   if ((t1i->_lo >= 0) &&
1149       (t1i->_hi > 0)  &&
1150       (t2i->_lo >= 0) &&
1151       (t2i->_hi > 0)) {
1152     // hi - set all bits below the highest bit. Using round_down to avoid overflow.
1153     const TypeInt* t1x = TypeInt::make(0, round_down_power_of_2(t1i->_hi) + (round_down_power_of_2(t1i->_hi) - 1), t1i->_widen);
1154     const TypeInt* t2x = TypeInt::make(0, round_down_power_of_2(t2i->_hi) + (round_down_power_of_2(t2i->_hi) - 1), t2i->_widen);
1155     return t1x->meet(t2x);
1156   }
1157   return AddNode::Value(phase);
1158 }
1159 
1160 
1161 //------------------------------add_ring---------------------------------------
1162 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
1163 // the logical operations the ring's ADD is really a logical OR function.
1164 // This also type-checks the inputs for sanity.  Guaranteed never to
1165 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
1166 const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
1167   const TypeInt *r0 = t0->is_int(); // Handy access
1168   const TypeInt *r1 = t1->is_int();
1169 
1170   // Complementing a boolean?
1171   if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE
1172                                || r1 == TypeInt::BOOL))
1173     return TypeInt::BOOL;
1174 
1175   if( !r0->is_con() || !r1->is_con() ) // Not constants
1176     return TypeInt::INT;        // Any integer, but still no symbols.
1177 
1178   // Otherwise just XOR them bits.
1179   return TypeInt::make( r0->get_con() ^ r1->get_con() );
1180 }
1181 
1182 //=============================================================================
1183 //------------------------------add_ring---------------------------------------
1184 const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
1185   const TypeLong *r0 = t0->is_long(); // Handy access
1186   const TypeLong *r1 = t1->is_long();
1187 
1188   // If either input is not a constant, just return all integers.
1189   if( !r0->is_con() || !r1->is_con() )
1190     return TypeLong::LONG;      // Any integer, but still no symbols.
1191 
1192   // Otherwise just OR them bits.
1193   return TypeLong::make( r0->get_con() ^ r1->get_con() );
1194 }
1195 
1196 Node* XorLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1197   Node* in1 = in(1);
1198   Node* in2 = in(2);
1199 
1200   // Convert ~x into -1-x when ~x is used in an arithmetic expression
1201   // or x itself is an arithmetic expression.
1202   if (phase->type(in2) == TypeLong::MINUS_1) { // follows LHS^(-1), i.e., ~LHS
1203     if (phase->is_IterGVN()) {
1204       if (is_used_in_only_arithmetic(this, T_LONG)
1205           // LHS is arithmetic
1206           || (in1->Opcode() == Op_AddL || in1->Opcode() == Op_SubL)) {
1207         return new SubLNode(in2, in1);
1208       }
1209     } else {
1210       // graph could be incomplete in GVN so we postpone to IGVN
1211       phase->record_for_igvn(this);
1212     }
1213   }
1214   return AddNode::Ideal(phase, can_reshape);
1215 }
1216 
1217 const Type* XorLNode::Value(PhaseGVN* phase) const {
1218   Node* in1 = in(1);
1219   Node* in2 = in(2);
1220   const Type* t1 = phase->type(in1);
1221   const Type* t2 = phase->type(in2);
1222   if (t1 == Type::TOP || t2 == Type::TOP) {
1223     return Type::TOP;
1224   }
1225   // x ^ x ==> 0
1226   if (in1->eqv_uncast(in2)) {
1227     return add_id();
1228   }
1229   // result of xor can only have bits sets where any of the
1230   // inputs have bits set. lo can always become 0.
1231   const TypeLong* t1l = t1->is_long();
1232   const TypeLong* t2l = t2->is_long();
1233   if ((t1l->_lo >= 0) &&
1234       (t1l->_hi > 0)  &&
1235       (t2l->_lo >= 0) &&
1236       (t2l->_hi > 0)) {
1237     // hi - set all bits below the highest bit. Using round_down to avoid overflow.
1238     const TypeLong* t1x = TypeLong::make(0, round_down_power_of_2(t1l->_hi) + (round_down_power_of_2(t1l->_hi) - 1), t1l->_widen);
1239     const TypeLong* t2x = TypeLong::make(0, round_down_power_of_2(t2l->_hi) + (round_down_power_of_2(t2l->_hi) - 1), t2l->_widen);
1240     return t1x->meet(t2x);
1241   }
1242   return AddNode::Value(phase);
1243 }
1244 
1245 Node* MaxNode::build_min_max_int(Node* a, Node* b, bool is_max) {
1246   if (is_max) {
1247     return new MaxINode(a, b);
1248   } else {
1249     return new MinINode(a, b);
1250   }
1251 }
1252 
1253 Node* MaxNode::build_min_max_long(PhaseGVN* phase, Node* a, Node* b, bool is_max) {
1254   if (is_max) {
1255     return new MaxLNode(phase->C, a, b);
1256   } else {
1257     return new MinLNode(phase->C, a, b);
1258   }
1259 }
1260 
1261 Node* MaxNode::build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn) {
1262   bool is_int = gvn.type(a)->isa_int();
1263   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1264   assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs");
1265   BasicType bt = is_int ? T_INT: T_LONG;
1266   Node* hook = nullptr;
1267   if (gvn.is_IterGVN()) {
1268     // Make sure a and b are not destroyed
1269     hook = new Node(2);
1270     hook->init_req(0, a);
1271     hook->init_req(1, b);
1272   }
1273   Node* res = nullptr;
1274   if (is_int && !is_unsigned) {
1275     res = gvn.transform(build_min_max_int(a, b, is_max));
1276     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");
1277   } else {
1278     Node* cmp = nullptr;
1279     if (is_max) {
1280       cmp = gvn.transform(CmpNode::make(a, b, bt, is_unsigned));
1281     } else {
1282       cmp = gvn.transform(CmpNode::make(b, a, bt, is_unsigned));
1283     }
1284     Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1285     res = gvn.transform(CMoveNode::make(nullptr, bol, a, b, t));
1286   }
1287   if (hook != nullptr) {
1288     hook->destruct(&gvn);
1289   }
1290   return res;
1291 }
1292 
1293 Node* MaxNode::build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn) {
1294   bool is_int = gvn.type(a)->isa_int();
1295   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1296   assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs");
1297   BasicType bt = is_int ? T_INT: T_LONG;
1298   Node* zero = gvn.integercon(0, bt);
1299   Node* hook = nullptr;
1300   if (gvn.is_IterGVN()) {
1301     // Make sure a and b are not destroyed
1302     hook = new Node(2);
1303     hook->init_req(0, a);
1304     hook->init_req(1, b);
1305   }
1306   Node* cmp = nullptr;
1307   if (is_max) {
1308     cmp = gvn.transform(CmpNode::make(a, b, bt, false));
1309   } else {
1310     cmp = gvn.transform(CmpNode::make(b, a, bt, false));
1311   }
1312   Node* sub = gvn.transform(SubNode::make(a, b, bt));
1313   Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1314   Node* res = gvn.transform(CMoveNode::make(nullptr, bol, sub, zero, t));
1315   if (hook != nullptr) {
1316     hook->destruct(&gvn);
1317   }
1318   return res;
1319 }
1320 
1321 // Check if addition of an integer with type 't' and a constant 'c' can overflow.
1322 static bool can_overflow(const TypeInt* t, jint c) {
1323   jint t_lo = t->_lo;
1324   jint t_hi = t->_hi;
1325   return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1326           (c > 0 && (java_add(t_hi, c) < t_hi)));
1327 }
1328 
1329 // Let <x, x_off> = x_operands and <y, y_off> = y_operands.
1330 // If x == y and neither add(x, x_off) nor add(y, y_off) overflow, return
1331 // add(x, op(x_off, y_off)). Otherwise, return nullptr.
1332 Node* MaxNode::extract_add(PhaseGVN* phase, ConstAddOperands x_operands, ConstAddOperands y_operands) {
1333   Node* x = x_operands.first;
1334   Node* y = y_operands.first;
1335   int opcode = Opcode();
1336   assert(opcode == Op_MaxI || opcode == Op_MinI, "Unexpected opcode");
1337   const TypeInt* tx = phase->type(x)->isa_int();
1338   jint x_off = x_operands.second;
1339   jint y_off = y_operands.second;
1340   if (x == y && tx != nullptr &&
1341       !can_overflow(tx, x_off) &&
1342       !can_overflow(tx, y_off)) {
1343     jint c = opcode == Op_MinI ? MIN2(x_off, y_off) : MAX2(x_off, y_off);
1344     return new AddINode(x, phase->intcon(c));
1345   }
1346   return nullptr;
1347 }
1348 
1349 // Try to cast n as an integer addition with a constant. Return:
1350 //   <x, C>,       if n == add(x, C), where 'C' is a non-TOP constant;
1351 //   <nullptr, 0>, if n == add(x, C), where 'C' is a TOP constant; or
1352 //   <n, 0>,       otherwise.
1353 static ConstAddOperands as_add_with_constant(Node* n) {
1354   if (n->Opcode() != Op_AddI) {
1355     return ConstAddOperands(n, 0);
1356   }
1357   Node* x = n->in(1);
1358   Node* c = n->in(2);
1359   if (!c->is_Con()) {
1360     return ConstAddOperands(n, 0);
1361   }
1362   const Type* c_type = c->bottom_type();
1363   if (c_type == Type::TOP) {
1364     return ConstAddOperands(nullptr, 0);
1365   }
1366   return ConstAddOperands(x, c_type->is_int()->get_con());
1367 }
1368 
1369 Node* MaxNode::IdealI(PhaseGVN* phase, bool can_reshape) {
1370   int opcode = Opcode();
1371   assert(opcode == Op_MinI || opcode == Op_MaxI, "Unexpected opcode");
1372   // Try to transform the following pattern, in any of its four possible
1373   // permutations induced by op's commutativity:
1374   //     op(op(add(inner, inner_off), inner_other), add(outer, outer_off))
1375   // into
1376   //     op(add(inner, op(inner_off, outer_off)), inner_other),
1377   // where:
1378   //     op is either MinI or MaxI, and
1379   //     inner == outer, and
1380   //     the additions cannot overflow.
1381   for (uint inner_op_index = 1; inner_op_index <= 2; inner_op_index++) {
1382     if (in(inner_op_index)->Opcode() != opcode) {
1383       continue;
1384     }
1385     Node* outer_add = in(inner_op_index == 1 ? 2 : 1);
1386     ConstAddOperands outer_add_operands = as_add_with_constant(outer_add);
1387     if (outer_add_operands.first == nullptr) {
1388       return nullptr; // outer_add has a TOP input, no need to continue.
1389     }
1390     // One operand is a MinI/MaxI and the other is an integer addition with
1391     // constant. Test the operands of the inner MinI/MaxI.
1392     for (uint inner_add_index = 1; inner_add_index <= 2; inner_add_index++) {
1393       Node* inner_op = in(inner_op_index);
1394       Node* inner_add = inner_op->in(inner_add_index);
1395       ConstAddOperands inner_add_operands = as_add_with_constant(inner_add);
1396       if (inner_add_operands.first == nullptr) {
1397         return nullptr; // inner_add has a TOP input, no need to continue.
1398       }
1399       // Try to extract the inner add.
1400       Node* add_extracted = extract_add(phase, inner_add_operands, outer_add_operands);
1401       if (add_extracted == nullptr) {
1402         continue;
1403       }
1404       Node* add_transformed = phase->transform(add_extracted);
1405       Node* inner_other = inner_op->in(inner_add_index == 1 ? 2 : 1);
1406       return build_min_max_int(add_transformed, inner_other, opcode == Op_MaxI);
1407     }
1408   }
1409   // Try to transform
1410   //     op(add(x, x_off), add(y, y_off))
1411   // into
1412   //     add(x, op(x_off, y_off)),
1413   // where:
1414   //     op is either MinI or MaxI, and
1415   //     inner == outer, and
1416   //     the additions cannot overflow.
1417   ConstAddOperands xC = as_add_with_constant(in(1));
1418   ConstAddOperands yC = as_add_with_constant(in(2));
1419   if (xC.first == nullptr || yC.first == nullptr) return nullptr;
1420   return extract_add(phase, xC, yC);
1421 }
1422 
1423 // Ideal transformations for MaxINode
1424 Node* MaxINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1425   return IdealI(phase, can_reshape);
1426 }
1427 
1428 //=============================================================================
1429 //------------------------------add_ring---------------------------------------
1430 // Supplied function returns the sum of the inputs.
1431 const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
1432   const TypeInt *r0 = t0->is_int(); // Handy access
1433   const TypeInt *r1 = t1->is_int();
1434 
1435   // Otherwise just MAX them bits.
1436   return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1437 }
1438 
1439 //=============================================================================
1440 //------------------------------Idealize---------------------------------------
1441 // MINs show up in range-check loop limit calculations.  Look for
1442 // "MIN2(x+c0,MIN2(y,x+c1))".  Pick the smaller constant: "MIN2(x+c0,y)"
1443 Node* MinINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1444   return IdealI(phase, can_reshape);
1445 }
1446 
1447 //------------------------------add_ring---------------------------------------
1448 // Supplied function returns the sum of the inputs.
1449 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
1450   const TypeInt *r0 = t0->is_int(); // Handy access
1451   const TypeInt *r1 = t1->is_int();
1452 
1453   // Otherwise just MIN them bits.
1454   return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1455 }
1456 
1457 // Collapse the "addition with overflow-protection" pattern, and the symmetrical
1458 // "subtraction with underflow-protection" pattern. These are created during the
1459 // unrolling, when we have to adjust the limit by subtracting the stride, but want
1460 // to protect against underflow: MaxL(SubL(limit, stride), min_jint).
1461 // If we have more than one of those in a sequence:
1462 //
1463 //   x  con2
1464 //   |  |
1465 //   AddL  clamp2
1466 //     |    |
1467 //    Max/MinL con1
1468 //          |  |
1469 //          AddL  clamp1
1470 //            |    |
1471 //           Max/MinL (n)
1472 //
1473 // We want to collapse it to:
1474 //
1475 //   x  con1  con2
1476 //   |    |    |
1477 //   |   AddLNode (new_con)
1478 //   |    |
1479 //  AddLNode  clamp1
1480 //        |    |
1481 //       Max/MinL (n)
1482 //
1483 // Note: we assume that SubL was already replaced by an AddL, and that the stride
1484 // has its sign flipped: SubL(limit, stride) -> AddL(limit, -stride).
1485 static Node* fold_subI_no_underflow_pattern(Node* n, PhaseGVN* phase) {
1486   assert(n->Opcode() == Op_MaxL || n->Opcode() == Op_MinL, "sanity");
1487   // Check that the two clamps have the correct values.
1488   jlong clamp = (n->Opcode() == Op_MaxL) ? min_jint : max_jint;
1489   auto is_clamp = [&](Node* c) {
1490     const TypeLong* t = phase->type(c)->isa_long();
1491     return t != nullptr && t->is_con() &&
1492            t->get_con() == clamp;
1493   };
1494   // Check that the constants are negative if MaxL, and positive if MinL.
1495   auto is_sub_con = [&](Node* c) {
1496     const TypeLong* t = phase->type(c)->isa_long();
1497     return t != nullptr && t->is_con() &&
1498            t->get_con() < max_jint && t->get_con() > min_jint &&
1499            (t->get_con() < 0) == (n->Opcode() == Op_MaxL);
1500   };
1501   // Verify the graph level by level:
1502   Node* add1   = n->in(1);
1503   Node* clamp1 = n->in(2);
1504   if (add1->Opcode() == Op_AddL && is_clamp(clamp1)) {
1505     Node* max2 = add1->in(1);
1506     Node* con1 = add1->in(2);
1507     if (max2->Opcode() == n->Opcode() && is_sub_con(con1)) {
1508       Node* add2   = max2->in(1);
1509       Node* clamp2 = max2->in(2);
1510       if (add2->Opcode() == Op_AddL && is_clamp(clamp2)) {
1511         Node* x    = add2->in(1);
1512         Node* con2 = add2->in(2);
1513         if (is_sub_con(con2)) {
1514           Node* new_con = phase->transform(new AddLNode(con1, con2));
1515           Node* new_sub = phase->transform(new AddLNode(x, new_con));
1516           n->set_req_X(1, new_sub, phase);
1517           return n;
1518         }
1519       }
1520     }
1521   }
1522   return nullptr;
1523 }
1524 
1525 const Type* MaxLNode::add_ring(const Type* t0, const Type* t1) const {
1526   const TypeLong* r0 = t0->is_long();
1527   const TypeLong* r1 = t1->is_long();
1528 
1529   return TypeLong::make(MAX2(r0->_lo, r1->_lo), MAX2(r0->_hi, r1->_hi), MAX2(r0->_widen, r1->_widen));
1530 }
1531 
1532 Node* MaxLNode::Identity(PhaseGVN* phase) {
1533   const TypeLong* t1 = phase->type(in(1))->is_long();
1534   const TypeLong* t2 = phase->type(in(2))->is_long();
1535 
1536   // Can we determine maximum statically?
1537   if (t1->_lo >= t2->_hi) {
1538     return in(1);
1539   } else if (t2->_lo >= t1->_hi) {
1540     return in(2);
1541   }
1542 
1543   return MaxNode::Identity(phase);
1544 }
1545 
1546 Node* MaxLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1547   Node* n = AddNode::Ideal(phase, can_reshape);
1548   if (n != nullptr) {
1549     return n;
1550   }
1551   if (can_reshape) {
1552     return fold_subI_no_underflow_pattern(this, phase);
1553   }
1554   return nullptr;
1555 }
1556 
1557 const Type* MinLNode::add_ring(const Type* t0, const Type* t1) const {
1558   const TypeLong* r0 = t0->is_long();
1559   const TypeLong* r1 = t1->is_long();
1560 
1561   return TypeLong::make(MIN2(r0->_lo, r1->_lo), MIN2(r0->_hi, r1->_hi), MIN2(r0->_widen, r1->_widen));
1562 }
1563 
1564 Node* MinLNode::Identity(PhaseGVN* phase) {
1565   const TypeLong* t1 = phase->type(in(1))->is_long();
1566   const TypeLong* t2 = phase->type(in(2))->is_long();
1567 
1568   // Can we determine minimum statically?
1569   if (t1->_lo >= t2->_hi) {
1570     return in(2);
1571   } else if (t2->_lo >= t1->_hi) {
1572     return in(1);
1573   }
1574 
1575   return MaxNode::Identity(phase);
1576 }
1577 
1578 Node* MinLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1579   Node* n = AddNode::Ideal(phase, can_reshape);
1580   if (n != nullptr) {
1581     return n;
1582   }
1583   if (can_reshape) {
1584     return fold_subI_no_underflow_pattern(this, phase);
1585   }
1586   return nullptr;
1587 }
1588 
1589 Node* MaxNode::Identity(PhaseGVN* phase) {
1590   if (in(1) == in(2)) {
1591       return in(1);
1592   }
1593 
1594   return AddNode::Identity(phase);
1595 }
1596 
1597 //------------------------------add_ring---------------------------------------
1598 const Type* MinFNode::add_ring(const Type* t0, const Type* t1 ) const {
1599   const TypeF* r0 = t0->isa_float_constant();
1600   const TypeF* r1 = t1->isa_float_constant();
1601   if (r0 == nullptr || r1 == nullptr) {
1602     return bottom_type();
1603   }
1604 
1605   if (r0->is_nan()) {
1606     return r0;
1607   }
1608   if (r1->is_nan()) {
1609     return r1;
1610   }
1611 
1612   float f0 = r0->getf();
1613   float f1 = r1->getf();
1614   if (f0 != 0.0f || f1 != 0.0f) {
1615     return f0 < f1 ? r0 : r1;
1616   }
1617 
1618   // handle min of 0.0, -0.0 case.
1619   return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1;
1620 }
1621 
1622 //------------------------------add_ring---------------------------------------
1623 const Type* MinDNode::add_ring(const Type* t0, const Type* t1) const {
1624   const TypeD* r0 = t0->isa_double_constant();
1625   const TypeD* r1 = t1->isa_double_constant();
1626   if (r0 == nullptr || r1 == nullptr) {
1627     return bottom_type();
1628   }
1629 
1630   if (r0->is_nan()) {
1631     return r0;
1632   }
1633   if (r1->is_nan()) {
1634     return r1;
1635   }
1636 
1637   double d0 = r0->getd();
1638   double d1 = r1->getd();
1639   if (d0 != 0.0 || d1 != 0.0) {
1640     return d0 < d1 ? r0 : r1;
1641   }
1642 
1643   // handle min of 0.0, -0.0 case.
1644   return (jlong_cast(d0) < jlong_cast(d1)) ? r0 : r1;
1645 }
1646 
1647 //------------------------------add_ring---------------------------------------
1648 const Type* MaxFNode::add_ring(const Type* t0, const Type* t1) const {
1649   const TypeF* r0 = t0->isa_float_constant();
1650   const TypeF* r1 = t1->isa_float_constant();
1651   if (r0 == nullptr || r1 == nullptr) {
1652     return bottom_type();
1653   }
1654 
1655   if (r0->is_nan()) {
1656     return r0;
1657   }
1658   if (r1->is_nan()) {
1659     return r1;
1660   }
1661 
1662   float f0 = r0->getf();
1663   float f1 = r1->getf();
1664   if (f0 != 0.0f || f1 != 0.0f) {
1665     return f0 > f1 ? r0 : r1;
1666   }
1667 
1668   // handle max of 0.0,-0.0 case.
1669   return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1;
1670 }
1671 
1672 //------------------------------add_ring---------------------------------------
1673 const Type* MaxDNode::add_ring(const Type* t0, const Type* t1) const {
1674   const TypeD* r0 = t0->isa_double_constant();
1675   const TypeD* r1 = t1->isa_double_constant();
1676   if (r0 == nullptr || r1 == nullptr) {
1677     return bottom_type();
1678   }
1679 
1680   if (r0->is_nan()) {
1681     return r0;
1682   }
1683   if (r1->is_nan()) {
1684     return r1;
1685   }
1686 
1687   double d0 = r0->getd();
1688   double d1 = r1->getd();
1689   if (d0 != 0.0 || d1 != 0.0) {
1690     return d0 > d1 ? r0 : r1;
1691   }
1692 
1693   // handle max of 0.0, -0.0 case.
1694   return (jlong_cast(d0) > jlong_cast(d1)) ? r0 : r1;
1695 }