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