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