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