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 if (tp->isa_aryptr()) { 664 // In the case of a flat inline type array, each field has its 665 // own slice so we need to extract the field being accessed from 666 // the address computation 667 return tp->is_aryptr()->add_field_offset_and_offset(txoffset); 668 } 669 return tp->add_offset(txoffset); 670 } 671 672 //------------------------------Value------------------------------------------ 673 const Type* AddPNode::Value(PhaseGVN* phase) const { 674 // Either input is TOP ==> the result is TOP 675 const Type *t1 = phase->type( in(Address) ); 676 const Type *t2 = phase->type( in(Offset) ); 677 if( t1 == Type::TOP ) return Type::TOP; 678 if( t2 == Type::TOP ) return Type::TOP; 679 680 // Left input is a pointer 681 const TypePtr *p1 = t1->isa_ptr(); 682 // Right input is an int 683 const TypeX *p2 = t2->is_intptr_t(); 684 // Add 'em 685 intptr_t p2offset = Type::OffsetBot; 686 if (p2->is_con()) { // Left input is an add of a constant? 687 p2offset = p2->get_con(); 688 } 689 if (p1->isa_aryptr()) { 690 // In the case of a flat inline type array, each field has its 691 // own slice so we need to extract the field being accessed from 692 // the address computation 693 return p1->is_aryptr()->add_field_offset_and_offset(p2offset); 694 } 695 return p1->add_offset(p2offset); 696 } 697 698 //------------------------Ideal_base_and_offset-------------------------------- 699 // Split an oop pointer into a base and offset. 700 // (The offset might be Type::OffsetBot in the case of an array.) 701 // Return the base, or null if failure. 702 Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseValues* phase, 703 // second return value: 704 intptr_t& offset) { 705 if (ptr->is_AddP()) { 706 Node* base = ptr->in(AddPNode::Base); 707 Node* addr = ptr->in(AddPNode::Address); 708 Node* offs = ptr->in(AddPNode::Offset); 709 if (base == addr || base->is_top()) { 710 offset = phase->find_intptr_t_con(offs, Type::OffsetBot); 711 if (offset != Type::OffsetBot) { 712 return addr; 713 } 714 } 715 } 716 offset = Type::OffsetBot; 717 return nullptr; 718 } 719 720 //------------------------------unpack_offsets---------------------------------- 721 // Collect the AddP offset values into the elements array, giving up 722 // if there are more than length. 723 int AddPNode::unpack_offsets(Node* elements[], int length) { 724 int count = 0; 725 Node* addr = this; 726 Node* base = addr->in(AddPNode::Base); 727 while (addr->is_AddP()) { 728 if (addr->in(AddPNode::Base) != base) { 729 // give up 730 return -1; 731 } 732 elements[count++] = addr->in(AddPNode::Offset); 733 if (count == length) { 734 // give up 735 return -1; 736 } 737 addr = addr->in(AddPNode::Address); 738 } 739 if (addr != base) { 740 return -1; 741 } 742 return count; 743 } 744 745 //------------------------------match_edge------------------------------------- 746 // Do we Match on this edge index or not? Do not match base pointer edge 747 uint AddPNode::match_edge(uint idx) const { 748 return idx > Base; 749 } 750 751 //============================================================================= 752 //------------------------------Identity--------------------------------------- 753 Node* OrINode::Identity(PhaseGVN* phase) { 754 // x | x => x 755 if (in(1) == in(2)) { 756 return in(1); 757 } 758 759 return AddNode::Identity(phase); 760 } 761 762 // Find shift value for Integer or Long OR. 763 Node* rotate_shift(PhaseGVN* phase, Node* lshift, Node* rshift, int mask) { 764 // val << norm_con_shift | val >> ({32|64} - norm_con_shift) => rotate_left val, norm_con_shift 765 const TypeInt* lshift_t = phase->type(lshift)->isa_int(); 766 const TypeInt* rshift_t = phase->type(rshift)->isa_int(); 767 if (lshift_t != nullptr && lshift_t->is_con() && 768 rshift_t != nullptr && rshift_t->is_con() && 769 ((lshift_t->get_con() & mask) == ((mask + 1) - (rshift_t->get_con() & mask)))) { 770 return phase->intcon(lshift_t->get_con() & mask); 771 } 772 // val << var_shift | val >> ({0|32|64} - var_shift) => rotate_left val, var_shift 773 if (rshift->Opcode() == Op_SubI && rshift->in(2) == lshift && rshift->in(1)->is_Con()){ 774 const TypeInt* shift_t = phase->type(rshift->in(1))->isa_int(); 775 if (shift_t != nullptr && shift_t->is_con() && 776 (shift_t->get_con() == 0 || shift_t->get_con() == (mask + 1))) { 777 return lshift; 778 } 779 } 780 return nullptr; 781 } 782 783 Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) { 784 int lopcode = in(1)->Opcode(); 785 int ropcode = in(2)->Opcode(); 786 if (Matcher::match_rule_supported(Op_RotateLeft) && 787 lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) { 788 Node* lshift = in(1)->in(2); 789 Node* rshift = in(2)->in(2); 790 Node* shift = rotate_shift(phase, lshift, rshift, 0x1F); 791 if (shift != nullptr) { 792 return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT); 793 } 794 return nullptr; 795 } 796 if (Matcher::match_rule_supported(Op_RotateRight) && 797 lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) { 798 Node* rshift = in(1)->in(2); 799 Node* lshift = in(2)->in(2); 800 Node* shift = rotate_shift(phase, rshift, lshift, 0x1F); 801 if (shift != nullptr) { 802 return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT); 803 } 804 } 805 return nullptr; 806 } 807 808 //------------------------------add_ring--------------------------------------- 809 // Supplied function returns the sum of the inputs IN THE CURRENT RING. For 810 // the logical operations the ring's ADD is really a logical OR function. 811 // This also type-checks the inputs for sanity. Guaranteed never to 812 // be passed a TOP or BOTTOM type, these are filtered out by pre-check. 813 const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const { 814 const TypeInt *r0 = t0->is_int(); // Handy access 815 const TypeInt *r1 = t1->is_int(); 816 817 // If both args are bool, can figure out better types 818 if ( r0 == TypeInt::BOOL ) { 819 if ( r1 == TypeInt::ONE) { 820 return TypeInt::ONE; 821 } else if ( r1 == TypeInt::BOOL ) { 822 return TypeInt::BOOL; 823 } 824 } else if ( r0 == TypeInt::ONE ) { 825 if ( r1 == TypeInt::BOOL ) { 826 return TypeInt::ONE; 827 } 828 } 829 830 // If either input is not a constant, just return all integers. 831 if( !r0->is_con() || !r1->is_con() ) 832 return TypeInt::INT; // Any integer, but still no symbols. 833 834 // Otherwise just OR them bits. 835 return TypeInt::make( r0->get_con() | r1->get_con() ); 836 } 837 838 //============================================================================= 839 //------------------------------Identity--------------------------------------- 840 Node* OrLNode::Identity(PhaseGVN* phase) { 841 // x | x => x 842 if (in(1) == in(2)) { 843 return in(1); 844 } 845 846 return AddNode::Identity(phase); 847 } 848 849 Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) { 850 int lopcode = in(1)->Opcode(); 851 int ropcode = in(2)->Opcode(); 852 if (Matcher::match_rule_supported(Op_RotateLeft) && 853 lopcode == Op_LShiftL && ropcode == Op_URShiftL && in(1)->in(1) == in(2)->in(1)) { 854 Node* lshift = in(1)->in(2); 855 Node* rshift = in(2)->in(2); 856 Node* shift = rotate_shift(phase, lshift, rshift, 0x3F); 857 if (shift != nullptr) { 858 return new RotateLeftNode(in(1)->in(1), shift, TypeLong::LONG); 859 } 860 return nullptr; 861 } 862 if (Matcher::match_rule_supported(Op_RotateRight) && 863 lopcode == Op_URShiftL && ropcode == Op_LShiftL && in(1)->in(1) == in(2)->in(1)) { 864 Node* rshift = in(1)->in(2); 865 Node* lshift = in(2)->in(2); 866 Node* shift = rotate_shift(phase, rshift, lshift, 0x3F); 867 if (shift != nullptr) { 868 return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG); 869 } 870 } 871 return nullptr; 872 } 873 874 //------------------------------add_ring--------------------------------------- 875 const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const { 876 const TypeLong *r0 = t0->is_long(); // Handy access 877 const TypeLong *r1 = t1->is_long(); 878 879 // If either input is not a constant, just return all integers. 880 if( !r0->is_con() || !r1->is_con() ) 881 return TypeLong::LONG; // Any integer, but still no symbols. 882 883 // Otherwise just OR them bits. 884 return TypeLong::make( r0->get_con() | r1->get_con() ); 885 } 886 887 //---------------------------Helper ------------------------------------------- 888 /* Decide if the given node is used only in arithmetic expressions(add or sub). 889 */ 890 static bool is_used_in_only_arithmetic(Node* n, BasicType bt) { 891 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 892 Node* u = n->fast_out(i); 893 if (u->Opcode() != Op_Add(bt) && u->Opcode() != Op_Sub(bt)) { 894 return false; 895 } 896 } 897 return true; 898 } 899 900 //============================================================================= 901 //------------------------------Idealize--------------------------------------- 902 Node* XorINode::Ideal(PhaseGVN* phase, bool can_reshape) { 903 Node* in1 = in(1); 904 Node* in2 = in(2); 905 906 // Convert ~x into -1-x when ~x is used in an arithmetic expression 907 // or x itself is an expression. 908 if (phase->type(in2) == TypeInt::MINUS_1) { // follows LHS^(-1), i.e., ~LHS 909 if (phase->is_IterGVN()) { 910 if (is_used_in_only_arithmetic(this, T_INT) 911 // LHS is arithmetic 912 || (in1->Opcode() == Op_AddI || in1->Opcode() == Op_SubI)) { 913 return new SubINode(in2, in1); 914 } 915 } else { 916 // graph could be incomplete in GVN so we postpone to IGVN 917 phase->record_for_igvn(this); 918 } 919 } 920 921 // Propagate xor through constant cmoves. This pattern can occur after expansion of Conv2B nodes. 922 const TypeInt* in2_type = phase->type(in2)->isa_int(); 923 if (in1->Opcode() == Op_CMoveI && in2_type != nullptr && in2_type->is_con()) { 924 int in2_val = in2_type->get_con(); 925 926 // Get types of both sides of the CMove 927 const TypeInt* left = phase->type(in1->in(CMoveNode::IfFalse))->isa_int(); 928 const TypeInt* right = phase->type(in1->in(CMoveNode::IfTrue))->isa_int(); 929 930 // Ensure that both sides are int constants 931 if (left != nullptr && right != nullptr && left->is_con() && right->is_con()) { 932 Node* cond = in1->in(CMoveNode::Condition); 933 934 // Check that the comparison is a bool and that the cmp node type is correct 935 if (cond->is_Bool()) { 936 int cmp_op = cond->in(1)->Opcode(); 937 938 if (cmp_op == Op_CmpI || cmp_op == Op_CmpP) { 939 int l_val = left->get_con(); 940 int r_val = right->get_con(); 941 942 return new CMoveINode(cond, phase->intcon(l_val ^ in2_val), phase->intcon(r_val ^ in2_val), TypeInt::INT); 943 } 944 } 945 } 946 } 947 948 return AddNode::Ideal(phase, can_reshape); 949 } 950 951 const Type* XorINode::Value(PhaseGVN* phase) const { 952 Node* in1 = in(1); 953 Node* in2 = in(2); 954 const Type* t1 = phase->type(in1); 955 const Type* t2 = phase->type(in2); 956 if (t1 == Type::TOP || t2 == Type::TOP) { 957 return Type::TOP; 958 } 959 // x ^ x ==> 0 960 if (in1->eqv_uncast(in2)) { 961 return add_id(); 962 } 963 // result of xor can only have bits sets where any of the 964 // inputs have bits set. lo can always become 0. 965 const TypeInt* t1i = t1->is_int(); 966 const TypeInt* t2i = t2->is_int(); 967 if ((t1i->_lo >= 0) && 968 (t1i->_hi > 0) && 969 (t2i->_lo >= 0) && 970 (t2i->_hi > 0)) { 971 // hi - set all bits below the highest bit. Using round_down to avoid overflow. 972 const TypeInt* t1x = TypeInt::make(0, round_down_power_of_2(t1i->_hi) + (round_down_power_of_2(t1i->_hi) - 1), t1i->_widen); 973 const TypeInt* t2x = TypeInt::make(0, round_down_power_of_2(t2i->_hi) + (round_down_power_of_2(t2i->_hi) - 1), t2i->_widen); 974 return t1x->meet(t2x); 975 } 976 return AddNode::Value(phase); 977 } 978 979 980 //------------------------------add_ring--------------------------------------- 981 // Supplied function returns the sum of the inputs IN THE CURRENT RING. For 982 // the logical operations the ring's ADD is really a logical OR function. 983 // This also type-checks the inputs for sanity. Guaranteed never to 984 // be passed a TOP or BOTTOM type, these are filtered out by pre-check. 985 const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const { 986 const TypeInt *r0 = t0->is_int(); // Handy access 987 const TypeInt *r1 = t1->is_int(); 988 989 // Complementing a boolean? 990 if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE 991 || r1 == TypeInt::BOOL)) 992 return TypeInt::BOOL; 993 994 if( !r0->is_con() || !r1->is_con() ) // Not constants 995 return TypeInt::INT; // Any integer, but still no symbols. 996 997 // Otherwise just XOR them bits. 998 return TypeInt::make( r0->get_con() ^ r1->get_con() ); 999 } 1000 1001 //============================================================================= 1002 //------------------------------add_ring--------------------------------------- 1003 const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const { 1004 const TypeLong *r0 = t0->is_long(); // Handy access 1005 const TypeLong *r1 = t1->is_long(); 1006 1007 // If either input is not a constant, just return all integers. 1008 if( !r0->is_con() || !r1->is_con() ) 1009 return TypeLong::LONG; // Any integer, but still no symbols. 1010 1011 // Otherwise just OR them bits. 1012 return TypeLong::make( r0->get_con() ^ r1->get_con() ); 1013 } 1014 1015 Node* XorLNode::Ideal(PhaseGVN* phase, bool can_reshape) { 1016 Node* in1 = in(1); 1017 Node* in2 = in(2); 1018 1019 // Convert ~x into -1-x when ~x is used in an arithmetic expression 1020 // or x itself is an arithmetic expression. 1021 if (phase->type(in2) == TypeLong::MINUS_1) { // follows LHS^(-1), i.e., ~LHS 1022 if (phase->is_IterGVN()) { 1023 if (is_used_in_only_arithmetic(this, T_LONG) 1024 // LHS is arithmetic 1025 || (in1->Opcode() == Op_AddL || in1->Opcode() == Op_SubL)) { 1026 return new SubLNode(in2, in1); 1027 } 1028 } else { 1029 // graph could be incomplete in GVN so we postpone to IGVN 1030 phase->record_for_igvn(this); 1031 } 1032 } 1033 return AddNode::Ideal(phase, can_reshape); 1034 } 1035 1036 const Type* XorLNode::Value(PhaseGVN* phase) const { 1037 Node* in1 = in(1); 1038 Node* in2 = in(2); 1039 const Type* t1 = phase->type(in1); 1040 const Type* t2 = phase->type(in2); 1041 if (t1 == Type::TOP || t2 == Type::TOP) { 1042 return Type::TOP; 1043 } 1044 // x ^ x ==> 0 1045 if (in1->eqv_uncast(in2)) { 1046 return add_id(); 1047 } 1048 // result of xor can only have bits sets where any of the 1049 // inputs have bits set. lo can always become 0. 1050 const TypeLong* t1l = t1->is_long(); 1051 const TypeLong* t2l = t2->is_long(); 1052 if ((t1l->_lo >= 0) && 1053 (t1l->_hi > 0) && 1054 (t2l->_lo >= 0) && 1055 (t2l->_hi > 0)) { 1056 // hi - set all bits below the highest bit. Using round_down to avoid overflow. 1057 const TypeLong* t1x = TypeLong::make(0, round_down_power_of_2(t1l->_hi) + (round_down_power_of_2(t1l->_hi) - 1), t1l->_widen); 1058 const TypeLong* t2x = TypeLong::make(0, round_down_power_of_2(t2l->_hi) + (round_down_power_of_2(t2l->_hi) - 1), t2l->_widen); 1059 return t1x->meet(t2x); 1060 } 1061 return AddNode::Value(phase); 1062 } 1063 1064 Node* build_min_max_int(Node* a, Node* b, bool is_max) { 1065 if (is_max) { 1066 return new MaxINode(a, b); 1067 } else { 1068 return new MinINode(a, b); 1069 } 1070 } 1071 1072 Node* MaxNode::build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn) { 1073 bool is_int = gvn.type(a)->isa_int(); 1074 assert(is_int || gvn.type(a)->isa_long(), "int or long inputs"); 1075 assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs"); 1076 BasicType bt = is_int ? T_INT: T_LONG; 1077 Node* hook = nullptr; 1078 if (gvn.is_IterGVN()) { 1079 // Make sure a and b are not destroyed 1080 hook = new Node(2); 1081 hook->init_req(0, a); 1082 hook->init_req(1, b); 1083 } 1084 Node* res = nullptr; 1085 if (is_int && !is_unsigned) { 1086 res = gvn.transform(build_min_max_int(a, b, is_max)); 1087 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"); 1088 } else { 1089 Node* cmp = nullptr; 1090 if (is_max) { 1091 cmp = gvn.transform(CmpNode::make(a, b, bt, is_unsigned)); 1092 } else { 1093 cmp = gvn.transform(CmpNode::make(b, a, bt, is_unsigned)); 1094 } 1095 Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt)); 1096 res = gvn.transform(CMoveNode::make(nullptr, bol, a, b, t)); 1097 } 1098 if (hook != nullptr) { 1099 hook->destruct(&gvn); 1100 } 1101 return res; 1102 } 1103 1104 Node* MaxNode::build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn) { 1105 bool is_int = gvn.type(a)->isa_int(); 1106 assert(is_int || gvn.type(a)->isa_long(), "int or long inputs"); 1107 assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs"); 1108 BasicType bt = is_int ? T_INT: T_LONG; 1109 Node* zero = gvn.integercon(0, bt); 1110 Node* hook = nullptr; 1111 if (gvn.is_IterGVN()) { 1112 // Make sure a and b are not destroyed 1113 hook = new Node(2); 1114 hook->init_req(0, a); 1115 hook->init_req(1, b); 1116 } 1117 Node* cmp = nullptr; 1118 if (is_max) { 1119 cmp = gvn.transform(CmpNode::make(a, b, bt, false)); 1120 } else { 1121 cmp = gvn.transform(CmpNode::make(b, a, bt, false)); 1122 } 1123 Node* sub = gvn.transform(SubNode::make(a, b, bt)); 1124 Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt)); 1125 Node* res = gvn.transform(CMoveNode::make(nullptr, bol, sub, zero, t)); 1126 if (hook != nullptr) { 1127 hook->destruct(&gvn); 1128 } 1129 return res; 1130 } 1131 1132 // Check if addition of an integer with type 't' and a constant 'c' can overflow. 1133 static bool can_overflow(const TypeInt* t, jint c) { 1134 jint t_lo = t->_lo; 1135 jint t_hi = t->_hi; 1136 return ((c < 0 && (java_add(t_lo, c) > t_lo)) || 1137 (c > 0 && (java_add(t_hi, c) < t_hi))); 1138 } 1139 1140 // Let <x, x_off> = x_operands and <y, y_off> = y_operands. 1141 // If x == y and neither add(x, x_off) nor add(y, y_off) overflow, return 1142 // add(x, op(x_off, y_off)). Otherwise, return nullptr. 1143 Node* MaxNode::extract_add(PhaseGVN* phase, ConstAddOperands x_operands, ConstAddOperands y_operands) { 1144 Node* x = x_operands.first; 1145 Node* y = y_operands.first; 1146 int opcode = Opcode(); 1147 assert(opcode == Op_MaxI || opcode == Op_MinI, "Unexpected opcode"); 1148 const TypeInt* tx = phase->type(x)->isa_int(); 1149 jint x_off = x_operands.second; 1150 jint y_off = y_operands.second; 1151 if (x == y && tx != nullptr && 1152 !can_overflow(tx, x_off) && 1153 !can_overflow(tx, y_off)) { 1154 jint c = opcode == Op_MinI ? MIN2(x_off, y_off) : MAX2(x_off, y_off); 1155 return new AddINode(x, phase->intcon(c)); 1156 } 1157 return nullptr; 1158 } 1159 1160 // Try to cast n as an integer addition with a constant. Return: 1161 // <x, C>, if n == add(x, C), where 'C' is a non-TOP constant; 1162 // <nullptr, 0>, if n == add(x, C), where 'C' is a TOP constant; or 1163 // <n, 0>, otherwise. 1164 static ConstAddOperands as_add_with_constant(Node* n) { 1165 if (n->Opcode() != Op_AddI) { 1166 return ConstAddOperands(n, 0); 1167 } 1168 Node* x = n->in(1); 1169 Node* c = n->in(2); 1170 if (!c->is_Con()) { 1171 return ConstAddOperands(n, 0); 1172 } 1173 const Type* c_type = c->bottom_type(); 1174 if (c_type == Type::TOP) { 1175 return ConstAddOperands(nullptr, 0); 1176 } 1177 return ConstAddOperands(x, c_type->is_int()->get_con()); 1178 } 1179 1180 Node* MaxNode::IdealI(PhaseGVN* phase, bool can_reshape) { 1181 int opcode = Opcode(); 1182 assert(opcode == Op_MinI || opcode == Op_MaxI, "Unexpected opcode"); 1183 // Try to transform the following pattern, in any of its four possible 1184 // permutations induced by op's commutativity: 1185 // op(op(add(inner, inner_off), inner_other), add(outer, outer_off)) 1186 // into 1187 // op(add(inner, op(inner_off, outer_off)), inner_other), 1188 // where: 1189 // op is either MinI or MaxI, and 1190 // inner == outer, and 1191 // the additions cannot overflow. 1192 for (uint inner_op_index = 1; inner_op_index <= 2; inner_op_index++) { 1193 if (in(inner_op_index)->Opcode() != opcode) { 1194 continue; 1195 } 1196 Node* outer_add = in(inner_op_index == 1 ? 2 : 1); 1197 ConstAddOperands outer_add_operands = as_add_with_constant(outer_add); 1198 if (outer_add_operands.first == nullptr) { 1199 return nullptr; // outer_add has a TOP input, no need to continue. 1200 } 1201 // One operand is a MinI/MaxI and the other is an integer addition with 1202 // constant. Test the operands of the inner MinI/MaxI. 1203 for (uint inner_add_index = 1; inner_add_index <= 2; inner_add_index++) { 1204 Node* inner_op = in(inner_op_index); 1205 Node* inner_add = inner_op->in(inner_add_index); 1206 ConstAddOperands inner_add_operands = as_add_with_constant(inner_add); 1207 if (inner_add_operands.first == nullptr) { 1208 return nullptr; // inner_add has a TOP input, no need to continue. 1209 } 1210 // Try to extract the inner add. 1211 Node* add_extracted = extract_add(phase, inner_add_operands, outer_add_operands); 1212 if (add_extracted == nullptr) { 1213 continue; 1214 } 1215 Node* add_transformed = phase->transform(add_extracted); 1216 Node* inner_other = inner_op->in(inner_add_index == 1 ? 2 : 1); 1217 return build_min_max_int(add_transformed, inner_other, opcode == Op_MaxI); 1218 } 1219 } 1220 // Try to transform 1221 // op(add(x, x_off), add(y, y_off)) 1222 // into 1223 // add(x, op(x_off, y_off)), 1224 // where: 1225 // op is either MinI or MaxI, and 1226 // inner == outer, and 1227 // the additions cannot overflow. 1228 ConstAddOperands xC = as_add_with_constant(in(1)); 1229 ConstAddOperands yC = as_add_with_constant(in(2)); 1230 if (xC.first == nullptr || yC.first == nullptr) return nullptr; 1231 return extract_add(phase, xC, yC); 1232 } 1233 1234 // Ideal transformations for MaxINode 1235 Node* MaxINode::Ideal(PhaseGVN* phase, bool can_reshape) { 1236 return IdealI(phase, can_reshape); 1237 } 1238 1239 //============================================================================= 1240 //------------------------------add_ring--------------------------------------- 1241 // Supplied function returns the sum of the inputs. 1242 const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const { 1243 const TypeInt *r0 = t0->is_int(); // Handy access 1244 const TypeInt *r1 = t1->is_int(); 1245 1246 // Otherwise just MAX them bits. 1247 return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) ); 1248 } 1249 1250 //============================================================================= 1251 //------------------------------Idealize--------------------------------------- 1252 // MINs show up in range-check loop limit calculations. Look for 1253 // "MIN2(x+c0,MIN2(y,x+c1))". Pick the smaller constant: "MIN2(x+c0,y)" 1254 Node* MinINode::Ideal(PhaseGVN* phase, bool can_reshape) { 1255 return IdealI(phase, can_reshape); 1256 } 1257 1258 //------------------------------add_ring--------------------------------------- 1259 // Supplied function returns the sum of the inputs. 1260 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const { 1261 const TypeInt *r0 = t0->is_int(); // Handy access 1262 const TypeInt *r1 = t1->is_int(); 1263 1264 // Otherwise just MIN them bits. 1265 return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) ); 1266 } 1267 1268 // Collapse the "addition with overflow-protection" pattern, and the symmetrical 1269 // "subtraction with underflow-protection" pattern. These are created during the 1270 // unrolling, when we have to adjust the limit by subtracting the stride, but want 1271 // to protect against underflow: MaxL(SubL(limit, stride), min_jint). 1272 // If we have more than one of those in a sequence: 1273 // 1274 // x con2 1275 // | | 1276 // AddL clamp2 1277 // | | 1278 // Max/MinL con1 1279 // | | 1280 // AddL clamp1 1281 // | | 1282 // Max/MinL (n) 1283 // 1284 // We want to collapse it to: 1285 // 1286 // x con1 con2 1287 // | | | 1288 // | AddLNode (new_con) 1289 // | | 1290 // AddLNode clamp1 1291 // | | 1292 // Max/MinL (n) 1293 // 1294 // Note: we assume that SubL was already replaced by an AddL, and that the stride 1295 // has its sign flipped: SubL(limit, stride) -> AddL(limit, -stride). 1296 Node* fold_subI_no_underflow_pattern(Node* n, PhaseGVN* phase) { 1297 assert(n->Opcode() == Op_MaxL || n->Opcode() == Op_MinL, "sanity"); 1298 // Check that the two clamps have the correct values. 1299 jlong clamp = (n->Opcode() == Op_MaxL) ? min_jint : max_jint; 1300 auto is_clamp = [&](Node* c) { 1301 const TypeLong* t = phase->type(c)->isa_long(); 1302 return t != nullptr && t->is_con() && 1303 t->get_con() == clamp; 1304 }; 1305 // Check that the constants are negative if MaxL, and positive if MinL. 1306 auto is_sub_con = [&](Node* c) { 1307 const TypeLong* t = phase->type(c)->isa_long(); 1308 return t != nullptr && t->is_con() && 1309 t->get_con() < max_jint && t->get_con() > min_jint && 1310 (t->get_con() < 0) == (n->Opcode() == Op_MaxL); 1311 }; 1312 // Verify the graph level by level: 1313 Node* add1 = n->in(1); 1314 Node* clamp1 = n->in(2); 1315 if (add1->Opcode() == Op_AddL && is_clamp(clamp1)) { 1316 Node* max2 = add1->in(1); 1317 Node* con1 = add1->in(2); 1318 if (max2->Opcode() == n->Opcode() && is_sub_con(con1)) { 1319 Node* add2 = max2->in(1); 1320 Node* clamp2 = max2->in(2); 1321 if (add2->Opcode() == Op_AddL && is_clamp(clamp2)) { 1322 Node* x = add2->in(1); 1323 Node* con2 = add2->in(2); 1324 if (is_sub_con(con2)) { 1325 Node* new_con = phase->transform(new AddLNode(con1, con2)); 1326 Node* new_sub = phase->transform(new AddLNode(x, new_con)); 1327 n->set_req_X(1, new_sub, phase); 1328 return n; 1329 } 1330 } 1331 } 1332 } 1333 return nullptr; 1334 } 1335 1336 const Type* MaxLNode::add_ring(const Type* t0, const Type* t1) const { 1337 const TypeLong* r0 = t0->is_long(); 1338 const TypeLong* r1 = t1->is_long(); 1339 1340 return TypeLong::make(MAX2(r0->_lo, r1->_lo), MAX2(r0->_hi, r1->_hi), MAX2(r0->_widen, r1->_widen)); 1341 } 1342 1343 Node* MaxLNode::Identity(PhaseGVN* phase) { 1344 const TypeLong* t1 = phase->type(in(1))->is_long(); 1345 const TypeLong* t2 = phase->type(in(2))->is_long(); 1346 1347 // Can we determine maximum statically? 1348 if (t1->_lo >= t2->_hi) { 1349 return in(1); 1350 } else if (t2->_lo >= t1->_hi) { 1351 return in(2); 1352 } 1353 1354 return MaxNode::Identity(phase); 1355 } 1356 1357 Node* MaxLNode::Ideal(PhaseGVN* phase, bool can_reshape) { 1358 Node* n = AddNode::Ideal(phase, can_reshape); 1359 if (n != nullptr) { 1360 return n; 1361 } 1362 if (can_reshape) { 1363 return fold_subI_no_underflow_pattern(this, phase); 1364 } 1365 return nullptr; 1366 } 1367 1368 const Type* MinLNode::add_ring(const Type* t0, const Type* t1) const { 1369 const TypeLong* r0 = t0->is_long(); 1370 const TypeLong* r1 = t1->is_long(); 1371 1372 return TypeLong::make(MIN2(r0->_lo, r1->_lo), MIN2(r0->_hi, r1->_hi), MIN2(r0->_widen, r1->_widen)); 1373 } 1374 1375 Node* MinLNode::Identity(PhaseGVN* phase) { 1376 const TypeLong* t1 = phase->type(in(1))->is_long(); 1377 const TypeLong* t2 = phase->type(in(2))->is_long(); 1378 1379 // Can we determine minimum statically? 1380 if (t1->_lo >= t2->_hi) { 1381 return in(2); 1382 } else if (t2->_lo >= t1->_hi) { 1383 return in(1); 1384 } 1385 1386 return MaxNode::Identity(phase); 1387 } 1388 1389 Node* MinLNode::Ideal(PhaseGVN* phase, bool can_reshape) { 1390 Node* n = AddNode::Ideal(phase, can_reshape); 1391 if (n != nullptr) { 1392 return n; 1393 } 1394 if (can_reshape) { 1395 return fold_subI_no_underflow_pattern(this, phase); 1396 } 1397 return nullptr; 1398 } 1399 1400 //------------------------------add_ring--------------------------------------- 1401 const Type* MinFNode::add_ring(const Type* t0, const Type* t1 ) const { 1402 const TypeF* r0 = t0->isa_float_constant(); 1403 const TypeF* r1 = t1->isa_float_constant(); 1404 if (r0 == nullptr || r1 == nullptr) { 1405 return bottom_type(); 1406 } 1407 1408 if (r0->is_nan()) { 1409 return r0; 1410 } 1411 if (r1->is_nan()) { 1412 return r1; 1413 } 1414 1415 float f0 = r0->getf(); 1416 float f1 = r1->getf(); 1417 if (f0 != 0.0f || f1 != 0.0f) { 1418 return f0 < f1 ? r0 : r1; 1419 } 1420 1421 // handle min of 0.0, -0.0 case. 1422 return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1; 1423 } 1424 1425 //------------------------------add_ring--------------------------------------- 1426 const Type* MinDNode::add_ring(const Type* t0, const Type* t1) const { 1427 const TypeD* r0 = t0->isa_double_constant(); 1428 const TypeD* r1 = t1->isa_double_constant(); 1429 if (r0 == nullptr || r1 == nullptr) { 1430 return bottom_type(); 1431 } 1432 1433 if (r0->is_nan()) { 1434 return r0; 1435 } 1436 if (r1->is_nan()) { 1437 return r1; 1438 } 1439 1440 double d0 = r0->getd(); 1441 double d1 = r1->getd(); 1442 if (d0 != 0.0 || d1 != 0.0) { 1443 return d0 < d1 ? r0 : r1; 1444 } 1445 1446 // handle min of 0.0, -0.0 case. 1447 return (jlong_cast(d0) < jlong_cast(d1)) ? r0 : r1; 1448 } 1449 1450 //------------------------------add_ring--------------------------------------- 1451 const Type* MaxFNode::add_ring(const Type* t0, const Type* t1) const { 1452 const TypeF* r0 = t0->isa_float_constant(); 1453 const TypeF* r1 = t1->isa_float_constant(); 1454 if (r0 == nullptr || r1 == nullptr) { 1455 return bottom_type(); 1456 } 1457 1458 if (r0->is_nan()) { 1459 return r0; 1460 } 1461 if (r1->is_nan()) { 1462 return r1; 1463 } 1464 1465 float f0 = r0->getf(); 1466 float f1 = r1->getf(); 1467 if (f0 != 0.0f || f1 != 0.0f) { 1468 return f0 > f1 ? r0 : r1; 1469 } 1470 1471 // handle max of 0.0,-0.0 case. 1472 return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1; 1473 } 1474 1475 //------------------------------add_ring--------------------------------------- 1476 const Type* MaxDNode::add_ring(const Type* t0, const Type* t1) const { 1477 const TypeD* r0 = t0->isa_double_constant(); 1478 const TypeD* r1 = t1->isa_double_constant(); 1479 if (r0 == nullptr || r1 == nullptr) { 1480 return bottom_type(); 1481 } 1482 1483 if (r0->is_nan()) { 1484 return r0; 1485 } 1486 if (r1->is_nan()) { 1487 return r1; 1488 } 1489 1490 double d0 = r0->getd(); 1491 double d1 = r1->getd(); 1492 if (d0 != 0.0 || d1 != 0.0) { 1493 return d0 > d1 ? r0 : r1; 1494 } 1495 1496 // handle max of 0.0, -0.0 case. 1497 return (jlong_cast(d0) > jlong_cast(d1)) ? r0 : r1; 1498 }