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