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