1 /* 2 * Copyright (c) 1997, 2022, 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 = NULL; // Progress flag 141 142 // Convert "(x+1)+2" into "x+(1+2)". If the right input is a 143 // constant, and the left input is an add of a constant, flatten the 144 // expression tree. 145 Node *add1 = in(1); 146 Node *add2 = in(2); 147 int add1_op = add1->Opcode(); 148 int this_op = Opcode(); 149 if (con_right && t2 != Type::TOP && // Right input is a constant? 150 add1_op == this_op) { // Left input is an Add? 151 152 // Type of left _in right input 153 const Type *t12 = phase->type(add1->in(2)); 154 if (t12->singleton() && t12 != Type::TOP) { // Left input is an add of a constant? 155 // Check for rare case of closed data cycle which can happen inside 156 // unreachable loops. In these cases the computation is undefined. 157 #ifdef ASSERT 158 Node *add11 = add1->in(1); 159 int add11_op = add11->Opcode(); 160 if ((add1 == add1->in(1)) 161 || (add11_op == this_op && add11->in(1) == add1)) { 162 assert(false, "dead loop in AddNode::Ideal"); 163 } 164 #endif 165 // The Add of the flattened expression 166 Node *x1 = add1->in(1); 167 Node *x2 = phase->makecon(add1->as_Add()->add_ring(t2, t12)); 168 set_req_X(2, x2, phase); 169 set_req_X(1, x1, phase); 170 progress = this; // Made progress 171 add1 = in(1); 172 add1_op = add1->Opcode(); 173 } 174 } 175 176 // Convert "(x+1)+y" into "(x+y)+1". Push constants down the expression tree. 177 if (add1_op == this_op && !con_right) { 178 Node *a12 = add1->in(2); 179 const Type *t12 = phase->type( a12 ); 180 if (t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) && 181 !(add1->in(1)->is_Phi() && (add1->in(1)->as_Phi()->is_tripcount(T_INT) || add1->in(1)->as_Phi()->is_tripcount(T_LONG)))) { 182 assert(add1->in(1) != this, "dead loop in AddNode::Ideal"); 183 add2 = add1->clone(); 184 add2->set_req(2, in(2)); 185 add2 = phase->transform(add2); 186 set_req_X(1, add2, phase); 187 set_req_X(2, a12, phase); 188 progress = this; 189 add2 = a12; 190 } 191 } 192 193 // Convert "x+(y+1)" into "(x+y)+1". Push constants down the expression tree. 194 int add2_op = add2->Opcode(); 195 if (add2_op == this_op && !con_left) { 196 Node *a22 = add2->in(2); 197 const Type *t22 = phase->type( a22 ); 198 if (t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) && 199 !(add2->in(1)->is_Phi() && (add2->in(1)->as_Phi()->is_tripcount(T_INT) || add2->in(1)->as_Phi()->is_tripcount(T_LONG)))) { 200 assert(add2->in(1) != this, "dead loop in AddNode::Ideal"); 201 Node *addx = add2->clone(); 202 addx->set_req(1, in(1)); 203 addx->set_req(2, add2->in(1)); 204 addx = phase->transform(addx); 205 set_req_X(1, addx, phase); 206 set_req_X(2, a22, phase); 207 progress = this; 208 } 209 } 210 211 return progress; 212 } 213 214 //------------------------------Value----------------------------------------- 215 // An add node sums it's two _in. If one input is an RSD, we must mixin 216 // the other input's symbols. 217 const Type* AddNode::Value(PhaseGVN* phase) const { 218 // Either input is TOP ==> the result is TOP 219 const Type *t1 = phase->type( in(1) ); 220 const Type *t2 = phase->type( in(2) ); 221 if( t1 == Type::TOP ) return Type::TOP; 222 if( t2 == Type::TOP ) return Type::TOP; 223 224 // Either input is BOTTOM ==> the result is the local BOTTOM 225 const Type *bot = bottom_type(); 226 if( (t1 == bot) || (t2 == bot) || 227 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 228 return bot; 229 230 // Check for an addition involving the additive identity 231 const Type *tadd = add_of_identity( t1, t2 ); 232 if( tadd ) return tadd; 233 234 return add_ring(t1,t2); // Local flavor of type addition 235 } 236 237 //------------------------------add_identity----------------------------------- 238 // Check for addition of the identity 239 const Type *AddNode::add_of_identity( const Type *t1, const Type *t2 ) const { 240 const Type *zero = add_id(); // The additive identity 241 if( t1->higher_equal( zero ) ) return t2; 242 if( t2->higher_equal( zero ) ) return t1; 243 244 return NULL; 245 } 246 247 AddNode* AddNode::make(Node* in1, Node* in2, BasicType bt) { 248 switch (bt) { 249 case T_INT: 250 return new AddINode(in1, in2); 251 case T_LONG: 252 return new AddLNode(in1, in2); 253 default: 254 fatal("Not implemented for %s", type2name(bt)); 255 } 256 return NULL; 257 } 258 259 //============================================================================= 260 //------------------------------Idealize--------------------------------------- 261 Node* AddNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) { 262 Node* in1 = in(1); 263 Node* in2 = in(2); 264 int op1 = in1->Opcode(); 265 int op2 = in2->Opcode(); 266 // Fold (con1-x)+con2 into (con1+con2)-x 267 if (op1 == Op_Add(bt) && op2 == Op_Sub(bt)) { 268 // Swap edges to try optimizations below 269 in1 = in2; 270 in2 = in(1); 271 op1 = op2; 272 op2 = in2->Opcode(); 273 } 274 if (op1 == Op_Sub(bt)) { 275 const Type* t_sub1 = phase->type(in1->in(1)); 276 const Type* t_2 = phase->type(in2 ); 277 if (t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP) { 278 return SubNode::make(phase->makecon(add_ring(t_sub1, t_2)), in1->in(2), bt); 279 } 280 // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)" 281 if (op2 == Op_Sub(bt)) { 282 // Check for dead cycle: d = (a-b)+(c-d) 283 assert( in1->in(2) != this && in2->in(2) != this, 284 "dead loop in AddINode::Ideal" ); 285 Node* sub = SubNode::make(NULL, NULL, bt); 286 sub->init_req(1, phase->transform(AddNode::make(in1->in(1), in2->in(1), bt))); 287 sub->init_req(2, phase->transform(AddNode::make(in1->in(2), in2->in(2), bt))); 288 return sub; 289 } 290 // Convert "(a-b)+(b+c)" into "(a+c)" 291 if (op2 == Op_Add(bt) && in1->in(2) == in2->in(1)) { 292 assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal/AddLNode::Ideal"); 293 return AddNode::make(in1->in(1), in2->in(2), bt); 294 } 295 // Convert "(a-b)+(c+b)" into "(a+c)" 296 if (op2 == Op_Add(bt) && in1->in(2) == in2->in(2)) { 297 assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal/AddLNode::Ideal"); 298 return AddNode::make(in1->in(1), in2->in(1), bt); 299 } 300 } 301 302 // Convert (con - y) + x into "(x - y) + con" 303 if (op1 == Op_Sub(bt) && in1->in(1)->Opcode() == Op_ConIL(bt) 304 && in1 != in1->in(2) && !(in1->in(2)->is_Phi() && in1->in(2)->as_Phi()->is_tripcount(bt))) { 305 return AddNode::make(phase->transform(SubNode::make(in2, in1->in(2), bt)), in1->in(1), bt); 306 } 307 308 // Convert x + (con - y) into "(x - y) + con" 309 if (op2 == Op_Sub(bt) && in2->in(1)->Opcode() == Op_ConIL(bt) 310 && in2 != in2->in(2) && !(in2->in(2)->is_Phi() && in2->in(2)->as_Phi()->is_tripcount(bt))) { 311 return AddNode::make(phase->transform(SubNode::make(in1, in2->in(2), bt)), in2->in(1), bt); 312 } 313 314 // Associative 315 if (op1 == Op_Mul(bt) && op2 == Op_Mul(bt)) { 316 Node* add_in1 = NULL; 317 Node* add_in2 = NULL; 318 Node* mul_in = NULL; 319 320 if (in1->in(1) == in2->in(1)) { 321 // Convert "a*b+a*c into a*(b+c) 322 add_in1 = in1->in(2); 323 add_in2 = in2->in(2); 324 mul_in = in1->in(1); 325 } else if (in1->in(2) == in2->in(1)) { 326 // Convert a*b+b*c into b*(a+c) 327 add_in1 = in1->in(1); 328 add_in2 = in2->in(2); 329 mul_in = in1->in(2); 330 } else if (in1->in(2) == in2->in(2)) { 331 // Convert a*c+b*c into (a+b)*c 332 add_in1 = in1->in(1); 333 add_in2 = in2->in(1); 334 mul_in = in1->in(2); 335 } else if (in1->in(1) == in2->in(2)) { 336 // Convert a*b+c*a into a*(b+c) 337 add_in1 = in1->in(2); 338 add_in2 = in2->in(1); 339 mul_in = in1->in(1); 340 } 341 342 if (mul_in != NULL) { 343 Node* add = phase->transform(AddNode::make(add_in1, add_in2, bt)); 344 return MulNode::make(mul_in, add, bt); 345 } 346 } 347 348 // Convert (x >>> rshift) + (x << lshift) into RotateRight(x, rshift) 349 if (Matcher::match_rule_supported(Op_RotateRight) && 350 ((op1 == Op_URShift(bt) && op2 == Op_LShift(bt)) || (op1 == Op_LShift(bt) && op2 == Op_URShift(bt))) && 351 in1->in(1) != NULL && in1->in(1) == in2->in(1)) { 352 Node* rshift = op1 == Op_URShift(bt) ? in1->in(2) : in2->in(2); 353 Node* lshift = op1 == Op_URShift(bt) ? in2->in(2) : in1->in(2); 354 if (rshift != NULL && lshift != NULL) { 355 const TypeInt* rshift_t = phase->type(rshift)->isa_int(); 356 const TypeInt* lshift_t = phase->type(lshift)->isa_int(); 357 int bits = bt == T_INT ? 32 : 64; 358 int mask = bt == T_INT ? 0x1F : 0x3F; 359 if (lshift_t != NULL && lshift_t->is_con() && 360 rshift_t != NULL && rshift_t->is_con() && 361 ((lshift_t->get_con() & mask) == (bits - (rshift_t->get_con() & mask)))) { 362 return new RotateRightNode(in1->in(1), phase->intcon(rshift_t->get_con() & mask), TypeInteger::bottom(bt)); 363 } 364 } 365 } 366 367 return AddNode::Ideal(phase, can_reshape); 368 } 369 370 371 Node* AddINode::Ideal(PhaseGVN* phase, bool can_reshape) { 372 Node* in1 = in(1); 373 Node* in2 = in(2); 374 int op1 = in1->Opcode(); 375 int op2 = in2->Opcode(); 376 377 // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y. 378 // Helps with array allocation math constant folding 379 // See 4790063: 380 // Unrestricted transformation is unsafe for some runtime values of 'x' 381 // ( x == 0, z == 1, y == -1 ) fails 382 // ( x == -5, z == 1, y == 1 ) fails 383 // Transform works for small z and small negative y when the addition 384 // (x + (y << z)) does not cross zero. 385 // Implement support for negative y and (x >= -(y << z)) 386 // Have not observed cases where type information exists to support 387 // positive y and (x <= -(y << z)) 388 if (op1 == Op_URShiftI && op2 == Op_ConI && 389 in1->in(2)->Opcode() == Op_ConI) { 390 jint z = phase->type(in1->in(2))->is_int()->get_con() & 0x1f; // only least significant 5 bits matter 391 jint y = phase->type(in2)->is_int()->get_con(); 392 393 if (z < 5 && -5 < y && y < 0) { 394 const Type* t_in11 = phase->type(in1->in(1)); 395 if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z))) { 396 Node* a = phase->transform(new AddINode( in1->in(1), phase->intcon(y<<z))); 397 return new URShiftINode(a, in1->in(2)); 398 } 399 } 400 } 401 402 return AddNode::IdealIL(phase, can_reshape, T_INT); 403 } 404 405 406 //------------------------------Identity--------------------------------------- 407 // Fold (x-y)+y OR y+(x-y) into x 408 Node* AddINode::Identity(PhaseGVN* phase) { 409 if (in(1)->Opcode() == Op_SubI && in(1)->in(2) == in(2)) { 410 return in(1)->in(1); 411 } else if (in(2)->Opcode() == Op_SubI && in(2)->in(2) == in(1)) { 412 return in(2)->in(1); 413 } 414 return AddNode::Identity(phase); 415 } 416 417 418 //------------------------------add_ring--------------------------------------- 419 // Supplied function returns the sum of the inputs. Guaranteed never 420 // to be passed a TOP or BOTTOM type, these are filtered out by 421 // pre-check. 422 const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const { 423 const TypeInt *r0 = t0->is_int(); // Handy access 424 const TypeInt *r1 = t1->is_int(); 425 int lo = java_add(r0->_lo, r1->_lo); 426 int hi = java_add(r0->_hi, r1->_hi); 427 if( !(r0->is_con() && r1->is_con()) ) { 428 // Not both constants, compute approximate result 429 if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) { 430 lo = min_jint; hi = max_jint; // Underflow on the low side 431 } 432 if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) { 433 lo = min_jint; hi = max_jint; // Overflow on the high side 434 } 435 if( lo > hi ) { // Handle overflow 436 lo = min_jint; hi = max_jint; 437 } 438 } else { 439 // both constants, compute precise result using 'lo' and 'hi' 440 // Semantics define overflow and underflow for integer addition 441 // as expected. In particular: 0x80000000 + 0x80000000 --> 0x0 442 } 443 return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) ); 444 } 445 446 447 //============================================================================= 448 //------------------------------Idealize--------------------------------------- 449 Node* AddLNode::Ideal(PhaseGVN* phase, bool can_reshape) { 450 return AddNode::IdealIL(phase, can_reshape, T_LONG); 451 } 452 453 454 //------------------------------Identity--------------------------------------- 455 // Fold (x-y)+y OR y+(x-y) into x 456 Node* AddLNode::Identity(PhaseGVN* phase) { 457 if (in(1)->Opcode() == Op_SubL && in(1)->in(2) == in(2)) { 458 return in(1)->in(1); 459 } else if (in(2)->Opcode() == Op_SubL && in(2)->in(2) == in(1)) { 460 return in(2)->in(1); 461 } 462 return AddNode::Identity(phase); 463 } 464 465 466 //------------------------------add_ring--------------------------------------- 467 // Supplied function returns the sum of the inputs. Guaranteed never 468 // to be passed a TOP or BOTTOM type, these are filtered out by 469 // pre-check. 470 const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const { 471 const TypeLong *r0 = t0->is_long(); // Handy access 472 const TypeLong *r1 = t1->is_long(); 473 jlong lo = java_add(r0->_lo, r1->_lo); 474 jlong hi = java_add(r0->_hi, r1->_hi); 475 if( !(r0->is_con() && r1->is_con()) ) { 476 // Not both constants, compute approximate result 477 if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) { 478 lo =min_jlong; hi = max_jlong; // Underflow on the low side 479 } 480 if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) { 481 lo = min_jlong; hi = max_jlong; // Overflow on the high side 482 } 483 if( lo > hi ) { // Handle overflow 484 lo = min_jlong; hi = max_jlong; 485 } 486 } else { 487 // both constants, compute precise result using 'lo' and 'hi' 488 // Semantics define overflow and underflow for integer addition 489 // as expected. In particular: 0x80000000 + 0x80000000 --> 0x0 490 } 491 return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) ); 492 } 493 494 495 //============================================================================= 496 //------------------------------add_of_identity-------------------------------- 497 // Check for addition of the identity 498 const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const { 499 // x ADD 0 should return x unless 'x' is a -zero 500 // 501 // const Type *zero = add_id(); // The additive identity 502 // jfloat f1 = t1->getf(); 503 // jfloat f2 = t2->getf(); 504 // 505 // if( t1->higher_equal( zero ) ) return t2; 506 // if( t2->higher_equal( zero ) ) return t1; 507 508 return NULL; 509 } 510 511 //------------------------------add_ring--------------------------------------- 512 // Supplied function returns the sum of the inputs. 513 // This also type-checks the inputs for sanity. Guaranteed never to 514 // be passed a TOP or BOTTOM type, these are filtered out by pre-check. 515 const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const { 516 // We must be adding 2 float constants. 517 return TypeF::make( t0->getf() + t1->getf() ); 518 } 519 520 //------------------------------Ideal------------------------------------------ 521 Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) { 522 // Floating point additions are not associative because of boundary conditions (infinity) 523 return commute(phase, this) ? this : NULL; 524 } 525 526 527 //============================================================================= 528 //------------------------------add_of_identity-------------------------------- 529 // Check for addition of the identity 530 const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const { 531 // x ADD 0 should return x unless 'x' is a -zero 532 // 533 // const Type *zero = add_id(); // The additive identity 534 // jfloat f1 = t1->getf(); 535 // jfloat f2 = t2->getf(); 536 // 537 // if( t1->higher_equal( zero ) ) return t2; 538 // if( t2->higher_equal( zero ) ) return t1; 539 540 return NULL; 541 } 542 //------------------------------add_ring--------------------------------------- 543 // Supplied function returns the sum of the inputs. 544 // This also type-checks the inputs for sanity. Guaranteed never to 545 // be passed a TOP or BOTTOM type, these are filtered out by pre-check. 546 const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const { 547 // We must be adding 2 double constants. 548 return TypeD::make( t0->getd() + t1->getd() ); 549 } 550 551 //------------------------------Ideal------------------------------------------ 552 Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) { 553 // Floating point additions are not associative because of boundary conditions (infinity) 554 return commute(phase, this) ? this : NULL; 555 } 556 557 558 //============================================================================= 559 //------------------------------Identity--------------------------------------- 560 // If one input is a constant 0, return the other input. 561 Node* AddPNode::Identity(PhaseGVN* phase) { 562 return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this; 563 } 564 565 //------------------------------Idealize--------------------------------------- 566 Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) { 567 // Bail out if dead inputs 568 if( phase->type( in(Address) ) == Type::TOP ) return NULL; 569 570 // If the left input is an add of a constant, flatten the expression tree. 571 const Node *n = in(Address); 572 if (n->is_AddP() && n->in(Base) == in(Base)) { 573 const AddPNode *addp = n->as_AddP(); // Left input is an AddP 574 assert( !addp->in(Address)->is_AddP() || 575 addp->in(Address)->as_AddP() != addp, 576 "dead loop in AddPNode::Ideal" ); 577 // Type of left input's right input 578 const Type *t = phase->type( addp->in(Offset) ); 579 if( t == Type::TOP ) return NULL; 580 const TypeX *t12 = t->is_intptr_t(); 581 if( t12->is_con() ) { // Left input is an add of a constant? 582 // If the right input is a constant, combine constants 583 const Type *temp_t2 = phase->type( in(Offset) ); 584 if( temp_t2 == Type::TOP ) return NULL; 585 const TypeX *t2 = temp_t2->is_intptr_t(); 586 Node* address; 587 Node* offset; 588 if( t2->is_con() ) { 589 // The Add of the flattened expression 590 address = addp->in(Address); 591 offset = phase->MakeConX(t2->get_con() + t12->get_con()); 592 } else { 593 // Else move the constant to the right. ((A+con)+B) into ((A+B)+con) 594 address = phase->transform(new AddPNode(in(Base),addp->in(Address),in(Offset))); 595 offset = addp->in(Offset); 596 } 597 set_req_X(Address, address, phase); 598 set_req_X(Offset, offset, phase); 599 return this; 600 } 601 } 602 603 // Raw pointers? 604 if( in(Base)->bottom_type() == Type::TOP ) { 605 // If this is a NULL+long form (from unsafe accesses), switch to a rawptr. 606 if (phase->type(in(Address)) == TypePtr::NULL_PTR) { 607 Node* offset = in(Offset); 608 return new CastX2PNode(offset); 609 } 610 } 611 612 // If the right is an add of a constant, push the offset down. 613 // Convert: (ptr + (offset+con)) into (ptr+offset)+con. 614 // The idea is to merge array_base+scaled_index groups together, 615 // and only have different constant offsets from the same base. 616 const Node *add = in(Offset); 617 if( add->Opcode() == Op_AddX && add->in(1) != add ) { 618 const Type *t22 = phase->type( add->in(2) ); 619 if( t22->singleton() && (t22 != Type::TOP) ) { // Right input is an add of a constant? 620 set_req(Address, phase->transform(new AddPNode(in(Base),in(Address),add->in(1)))); 621 set_req_X(Offset, add->in(2), phase); // puts add on igvn worklist if needed 622 return this; // Made progress 623 } 624 } 625 626 return NULL; // No progress 627 } 628 629 //------------------------------bottom_type------------------------------------ 630 // Bottom-type is the pointer-type with unknown offset. 631 const Type *AddPNode::bottom_type() const { 632 if (in(Address) == NULL) return TypePtr::BOTTOM; 633 const TypePtr *tp = in(Address)->bottom_type()->isa_ptr(); 634 if( !tp ) return Type::TOP; // TOP input means TOP output 635 assert( in(Offset)->Opcode() != Op_ConP, "" ); 636 const Type *t = in(Offset)->bottom_type(); 637 if( t == Type::TOP ) 638 return tp->add_offset(Type::OffsetTop); 639 const TypeX *tx = t->is_intptr_t(); 640 intptr_t txoffset = Type::OffsetBot; 641 if (tx->is_con()) { // Left input is an add of a constant? 642 txoffset = tx->get_con(); 643 } 644 if (tp->isa_aryptr()) { 645 // In the case of a flattened inline type array, each field has its 646 // own slice so we need to extract the field being accessed from 647 // the address computation 648 return tp->is_aryptr()->add_field_offset_and_offset(txoffset); 649 } 650 return tp->add_offset(txoffset); 651 } 652 653 //------------------------------Value------------------------------------------ 654 const Type* AddPNode::Value(PhaseGVN* phase) const { 655 // Either input is TOP ==> the result is TOP 656 const Type *t1 = phase->type( in(Address) ); 657 const Type *t2 = phase->type( in(Offset) ); 658 if( t1 == Type::TOP ) return Type::TOP; 659 if( t2 == Type::TOP ) return Type::TOP; 660 661 // Left input is a pointer 662 const TypePtr *p1 = t1->isa_ptr(); 663 // Right input is an int 664 const TypeX *p2 = t2->is_intptr_t(); 665 // Add 'em 666 intptr_t p2offset = Type::OffsetBot; 667 if (p2->is_con()) { // Left input is an add of a constant? 668 p2offset = p2->get_con(); 669 } 670 if (p1->isa_aryptr()) { 671 // In the case of a flattened inline type array, each field has its 672 // own slice so we need to extract the field being accessed from 673 // the address computation 674 return p1->is_aryptr()->add_field_offset_and_offset(p2offset); 675 } 676 return p1->add_offset(p2offset); 677 } 678 679 //------------------------Ideal_base_and_offset-------------------------------- 680 // Split an oop pointer into a base and offset. 681 // (The offset might be Type::OffsetBot in the case of an array.) 682 // Return the base, or NULL if failure. 683 Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseTransform* phase, 684 // second return value: 685 intptr_t& offset) { 686 if (ptr->is_AddP()) { 687 Node* base = ptr->in(AddPNode::Base); 688 Node* addr = ptr->in(AddPNode::Address); 689 Node* offs = ptr->in(AddPNode::Offset); 690 if (base == addr || base->is_top()) { 691 offset = phase->find_intptr_t_con(offs, Type::OffsetBot); 692 if (offset != Type::OffsetBot) { 693 return addr; 694 } 695 } 696 } 697 offset = Type::OffsetBot; 698 return NULL; 699 } 700 701 //------------------------------unpack_offsets---------------------------------- 702 // Collect the AddP offset values into the elements array, giving up 703 // if there are more than length. 704 int AddPNode::unpack_offsets(Node* elements[], int length) { 705 int count = 0; 706 Node* addr = this; 707 Node* base = addr->in(AddPNode::Base); 708 while (addr->is_AddP()) { 709 if (addr->in(AddPNode::Base) != base) { 710 // give up 711 return -1; 712 } 713 elements[count++] = addr->in(AddPNode::Offset); 714 if (count == length) { 715 // give up 716 return -1; 717 } 718 addr = addr->in(AddPNode::Address); 719 } 720 if (addr != base) { 721 return -1; 722 } 723 return count; 724 } 725 726 //------------------------------match_edge------------------------------------- 727 // Do we Match on this edge index or not? Do not match base pointer edge 728 uint AddPNode::match_edge(uint idx) const { 729 return idx > Base; 730 } 731 732 //============================================================================= 733 //------------------------------Identity--------------------------------------- 734 Node* OrINode::Identity(PhaseGVN* phase) { 735 // x | x => x 736 if (in(1) == in(2)) { 737 return in(1); 738 } 739 740 return AddNode::Identity(phase); 741 } 742 743 // Find shift value for Integer or Long OR. 744 Node* rotate_shift(PhaseGVN* phase, Node* lshift, Node* rshift, int mask) { 745 // val << norm_con_shift | val >> ({32|64} - norm_con_shift) => rotate_left val, norm_con_shift 746 const TypeInt* lshift_t = phase->type(lshift)->isa_int(); 747 const TypeInt* rshift_t = phase->type(rshift)->isa_int(); 748 if (lshift_t != NULL && lshift_t->is_con() && 749 rshift_t != NULL && rshift_t->is_con() && 750 ((lshift_t->get_con() & mask) == ((mask + 1) - (rshift_t->get_con() & mask)))) { 751 return phase->intcon(lshift_t->get_con() & mask); 752 } 753 // val << var_shift | val >> ({0|32|64} - var_shift) => rotate_left val, var_shift 754 if (rshift->Opcode() == Op_SubI && rshift->in(2) == lshift && rshift->in(1)->is_Con()){ 755 const TypeInt* shift_t = phase->type(rshift->in(1))->isa_int(); 756 if (shift_t != NULL && shift_t->is_con() && 757 (shift_t->get_con() == 0 || shift_t->get_con() == (mask + 1))) { 758 return lshift; 759 } 760 } 761 return NULL; 762 } 763 764 Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) { 765 int lopcode = in(1)->Opcode(); 766 int ropcode = in(2)->Opcode(); 767 if (Matcher::match_rule_supported(Op_RotateLeft) && 768 lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) { 769 Node* lshift = in(1)->in(2); 770 Node* rshift = in(2)->in(2); 771 Node* shift = rotate_shift(phase, lshift, rshift, 0x1F); 772 if (shift != NULL) { 773 return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT); 774 } 775 return NULL; 776 } 777 if (Matcher::match_rule_supported(Op_RotateRight) && 778 lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) { 779 Node* rshift = in(1)->in(2); 780 Node* lshift = in(2)->in(2); 781 Node* shift = rotate_shift(phase, rshift, lshift, 0x1F); 782 if (shift != NULL) { 783 return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT); 784 } 785 } 786 return NULL; 787 } 788 789 //------------------------------add_ring--------------------------------------- 790 // Supplied function returns the sum of the inputs IN THE CURRENT RING. For 791 // the logical operations the ring's ADD is really a logical OR function. 792 // This also type-checks the inputs for sanity. Guaranteed never to 793 // be passed a TOP or BOTTOM type, these are filtered out by pre-check. 794 const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const { 795 const TypeInt *r0 = t0->is_int(); // Handy access 796 const TypeInt *r1 = t1->is_int(); 797 798 // If both args are bool, can figure out better types 799 if ( r0 == TypeInt::BOOL ) { 800 if ( r1 == TypeInt::ONE) { 801 return TypeInt::ONE; 802 } else if ( r1 == TypeInt::BOOL ) { 803 return TypeInt::BOOL; 804 } 805 } else if ( r0 == TypeInt::ONE ) { 806 if ( r1 == TypeInt::BOOL ) { 807 return TypeInt::ONE; 808 } 809 } 810 811 // If either input is not a constant, just return all integers. 812 if( !r0->is_con() || !r1->is_con() ) 813 return TypeInt::INT; // Any integer, but still no symbols. 814 815 // Otherwise just OR them bits. 816 return TypeInt::make( r0->get_con() | r1->get_con() ); 817 } 818 819 //============================================================================= 820 //------------------------------Identity--------------------------------------- 821 Node* OrLNode::Identity(PhaseGVN* phase) { 822 // x | x => x 823 if (in(1) == in(2)) { 824 return in(1); 825 } 826 827 return AddNode::Identity(phase); 828 } 829 830 Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) { 831 int lopcode = in(1)->Opcode(); 832 int ropcode = in(2)->Opcode(); 833 if (Matcher::match_rule_supported(Op_RotateLeft) && 834 lopcode == Op_LShiftL && ropcode == Op_URShiftL && in(1)->in(1) == in(2)->in(1)) { 835 Node* lshift = in(1)->in(2); 836 Node* rshift = in(2)->in(2); 837 Node* shift = rotate_shift(phase, lshift, rshift, 0x3F); 838 if (shift != NULL) { 839 return new RotateLeftNode(in(1)->in(1), shift, TypeLong::LONG); 840 } 841 return NULL; 842 } 843 if (Matcher::match_rule_supported(Op_RotateRight) && 844 lopcode == Op_URShiftL && ropcode == Op_LShiftL && in(1)->in(1) == in(2)->in(1)) { 845 Node* rshift = in(1)->in(2); 846 Node* lshift = in(2)->in(2); 847 Node* shift = rotate_shift(phase, rshift, lshift, 0x3F); 848 if (shift != NULL) { 849 return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG); 850 } 851 } 852 return NULL; 853 } 854 855 //------------------------------add_ring--------------------------------------- 856 const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const { 857 const TypeLong *r0 = t0->is_long(); // Handy access 858 const TypeLong *r1 = t1->is_long(); 859 860 // If either input is not a constant, just return all integers. 861 if( !r0->is_con() || !r1->is_con() ) 862 return TypeLong::LONG; // Any integer, but still no symbols. 863 864 // Otherwise just OR them bits. 865 return TypeLong::make( r0->get_con() | r1->get_con() ); 866 } 867 868 //---------------------------Helper ------------------------------------------- 869 /* Decide if the given node is used only in arithmetic expressions(add or sub). 870 */ 871 static bool is_used_in_only_arithmetic(Node* n, BasicType bt) { 872 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 873 Node* u = n->fast_out(i); 874 if (u->Opcode() != Op_Add(bt) && u->Opcode() != Op_Sub(bt)) { 875 return false; 876 } 877 } 878 return true; 879 } 880 881 //============================================================================= 882 //------------------------------Idealize--------------------------------------- 883 Node* XorINode::Ideal(PhaseGVN* phase, bool can_reshape) { 884 Node* in1 = in(1); 885 Node* in2 = in(2); 886 887 // Convert ~x into -1-x when ~x is used in an arithmetic expression 888 // or x itself is an expression. 889 if (phase->type(in2) == TypeInt::MINUS_1) { // follows LHS^(-1), i.e., ~LHS 890 if (phase->is_IterGVN()) { 891 if (is_used_in_only_arithmetic(this, T_INT) 892 // LHS is arithmetic 893 || (in1->Opcode() == Op_AddI || in1->Opcode() == Op_SubI)) { 894 return new SubINode(in2, in1); 895 } 896 } else { 897 // graph could be incomplete in GVN so we postpone to IGVN 898 phase->record_for_igvn(this); 899 } 900 } 901 return AddNode::Ideal(phase, can_reshape); 902 } 903 904 const Type* XorINode::Value(PhaseGVN* phase) const { 905 Node* in1 = in(1); 906 Node* in2 = in(2); 907 const Type* t1 = phase->type(in1); 908 const Type* t2 = phase->type(in2); 909 if (t1 == Type::TOP || t2 == Type::TOP) { 910 return Type::TOP; 911 } 912 // x ^ x ==> 0 913 if (in1->eqv_uncast(in2)) { 914 return add_id(); 915 } 916 // result of xor can only have bits sets where any of the 917 // inputs have bits set. lo can always become 0. 918 const TypeInt* t1i = t1->is_int(); 919 const TypeInt* t2i = t2->is_int(); 920 if ((t1i->_lo >= 0) && 921 (t1i->_hi > 0) && 922 (t2i->_lo >= 0) && 923 (t2i->_hi > 0)) { 924 // hi - set all bits below the highest bit. Using round_down to avoid overflow. 925 const TypeInt* t1x = TypeInt::make(0, round_down_power_of_2(t1i->_hi) + (round_down_power_of_2(t1i->_hi) - 1), t1i->_widen); 926 const TypeInt* t2x = TypeInt::make(0, round_down_power_of_2(t2i->_hi) + (round_down_power_of_2(t2i->_hi) - 1), t2i->_widen); 927 return t1x->meet(t2x); 928 } 929 return AddNode::Value(phase); 930 } 931 932 933 //------------------------------add_ring--------------------------------------- 934 // Supplied function returns the sum of the inputs IN THE CURRENT RING. For 935 // the logical operations the ring's ADD is really a logical OR function. 936 // This also type-checks the inputs for sanity. Guaranteed never to 937 // be passed a TOP or BOTTOM type, these are filtered out by pre-check. 938 const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const { 939 const TypeInt *r0 = t0->is_int(); // Handy access 940 const TypeInt *r1 = t1->is_int(); 941 942 // Complementing a boolean? 943 if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE 944 || r1 == TypeInt::BOOL)) 945 return TypeInt::BOOL; 946 947 if( !r0->is_con() || !r1->is_con() ) // Not constants 948 return TypeInt::INT; // Any integer, but still no symbols. 949 950 // Otherwise just XOR them bits. 951 return TypeInt::make( r0->get_con() ^ r1->get_con() ); 952 } 953 954 //============================================================================= 955 //------------------------------add_ring--------------------------------------- 956 const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const { 957 const TypeLong *r0 = t0->is_long(); // Handy access 958 const TypeLong *r1 = t1->is_long(); 959 960 // If either input is not a constant, just return all integers. 961 if( !r0->is_con() || !r1->is_con() ) 962 return TypeLong::LONG; // Any integer, but still no symbols. 963 964 // Otherwise just OR them bits. 965 return TypeLong::make( r0->get_con() ^ r1->get_con() ); 966 } 967 968 Node* XorLNode::Ideal(PhaseGVN* phase, bool can_reshape) { 969 Node* in1 = in(1); 970 Node* in2 = in(2); 971 972 // Convert ~x into -1-x when ~x is used in an arithmetic expression 973 // or x itself is an arithmetic expression. 974 if (phase->type(in2) == TypeLong::MINUS_1) { // follows LHS^(-1), i.e., ~LHS 975 if (phase->is_IterGVN()) { 976 if (is_used_in_only_arithmetic(this, T_LONG) 977 // LHS is arithmetic 978 || (in1->Opcode() == Op_AddL || in1->Opcode() == Op_SubL)) { 979 return new SubLNode(in2, in1); 980 } 981 } else { 982 // graph could be incomplete in GVN so we postpone to IGVN 983 phase->record_for_igvn(this); 984 } 985 } 986 return AddNode::Ideal(phase, can_reshape); 987 } 988 989 const Type* XorLNode::Value(PhaseGVN* phase) const { 990 Node* in1 = in(1); 991 Node* in2 = in(2); 992 const Type* t1 = phase->type(in1); 993 const Type* t2 = phase->type(in2); 994 if (t1 == Type::TOP || t2 == Type::TOP) { 995 return Type::TOP; 996 } 997 // x ^ x ==> 0 998 if (in1->eqv_uncast(in2)) { 999 return add_id(); 1000 } 1001 // result of xor can only have bits sets where any of the 1002 // inputs have bits set. lo can always become 0. 1003 const TypeLong* t1l = t1->is_long(); 1004 const TypeLong* t2l = t2->is_long(); 1005 if ((t1l->_lo >= 0) && 1006 (t1l->_hi > 0) && 1007 (t2l->_lo >= 0) && 1008 (t2l->_hi > 0)) { 1009 // hi - set all bits below the highest bit. Using round_down to avoid overflow. 1010 const TypeLong* t1x = TypeLong::make(0, round_down_power_of_2(t1l->_hi) + (round_down_power_of_2(t1l->_hi) - 1), t1l->_widen); 1011 const TypeLong* t2x = TypeLong::make(0, round_down_power_of_2(t2l->_hi) + (round_down_power_of_2(t2l->_hi) - 1), t2l->_widen); 1012 return t1x->meet(t2x); 1013 } 1014 return AddNode::Value(phase); 1015 } 1016 1017 Node* MaxNode::build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn) { 1018 bool is_int = gvn.type(a)->isa_int(); 1019 assert(is_int || gvn.type(a)->isa_long(), "int or long inputs"); 1020 assert(is_int == (gvn.type(b)->isa_int() != NULL), "inconsistent inputs"); 1021 BasicType bt = is_int ? T_INT: T_LONG; 1022 Node* hook = NULL; 1023 if (gvn.is_IterGVN()) { 1024 // Make sure a and b are not destroyed 1025 hook = new Node(2); 1026 hook->init_req(0, a); 1027 hook->init_req(1, b); 1028 } 1029 Node* res = NULL; 1030 if (is_int && !is_unsigned) { 1031 if (is_max) { 1032 res = gvn.transform(new MaxINode(a, b)); 1033 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"); 1034 } else { 1035 Node* res = gvn.transform(new MinINode(a, b)); 1036 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"); 1037 } 1038 } else { 1039 Node* cmp = NULL; 1040 if (is_max) { 1041 cmp = gvn.transform(CmpNode::make(a, b, bt, is_unsigned)); 1042 } else { 1043 cmp = gvn.transform(CmpNode::make(b, a, bt, is_unsigned)); 1044 } 1045 Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt)); 1046 res = gvn.transform(CMoveNode::make(NULL, bol, a, b, t)); 1047 } 1048 if (hook != NULL) { 1049 hook->destruct(&gvn); 1050 } 1051 return res; 1052 } 1053 1054 Node* MaxNode::build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn) { 1055 bool is_int = gvn.type(a)->isa_int(); 1056 assert(is_int || gvn.type(a)->isa_long(), "int or long inputs"); 1057 assert(is_int == (gvn.type(b)->isa_int() != NULL), "inconsistent inputs"); 1058 BasicType bt = is_int ? T_INT: T_LONG; 1059 Node* zero = gvn.integercon(0, bt); 1060 Node* hook = NULL; 1061 if (gvn.is_IterGVN()) { 1062 // Make sure a and b are not destroyed 1063 hook = new Node(2); 1064 hook->init_req(0, a); 1065 hook->init_req(1, b); 1066 } 1067 Node* cmp = NULL; 1068 if (is_max) { 1069 cmp = gvn.transform(CmpNode::make(a, b, bt, false)); 1070 } else { 1071 cmp = gvn.transform(CmpNode::make(b, a, bt, false)); 1072 } 1073 Node* sub = gvn.transform(SubNode::make(a, b, bt)); 1074 Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt)); 1075 Node* res = gvn.transform(CMoveNode::make(NULL, bol, sub, zero, t)); 1076 if (hook != NULL) { 1077 hook->destruct(&gvn); 1078 } 1079 return res; 1080 } 1081 1082 //============================================================================= 1083 //------------------------------add_ring--------------------------------------- 1084 // Supplied function returns the sum of the inputs. 1085 const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const { 1086 const TypeInt *r0 = t0->is_int(); // Handy access 1087 const TypeInt *r1 = t1->is_int(); 1088 1089 // Otherwise just MAX them bits. 1090 return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) ); 1091 } 1092 1093 // Check if addition of an integer with type 't' and a constant 'c' can overflow 1094 static bool can_overflow(const TypeInt* t, jint c) { 1095 jint t_lo = t->_lo; 1096 jint t_hi = t->_hi; 1097 return ((c < 0 && (java_add(t_lo, c) > t_lo)) || 1098 (c > 0 && (java_add(t_hi, c) < t_hi))); 1099 } 1100 1101 // Ideal transformations for MaxINode 1102 Node* MaxINode::Ideal(PhaseGVN* phase, bool can_reshape) { 1103 // Force a right-spline graph 1104 Node* l = in(1); 1105 Node* r = in(2); 1106 // Transform MaxI1(MaxI2(a, b), c) into MaxI1(a, MaxI2(b, c)) 1107 // to force a right-spline graph for the rest of MaxINode::Ideal(). 1108 if (l->Opcode() == Op_MaxI) { 1109 assert(l != l->in(1), "dead loop in MaxINode::Ideal"); 1110 r = phase->transform(new MaxINode(l->in(2), r)); 1111 l = l->in(1); 1112 set_req_X(1, l, phase); 1113 set_req_X(2, r, phase); 1114 return this; 1115 } 1116 1117 // Get left input & constant 1118 Node* x = l; 1119 jint x_off = 0; 1120 if (x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant 1121 x->in(2)->is_Con()) { 1122 const Type* t = x->in(2)->bottom_type(); 1123 if (t == Type::TOP) return NULL; // No progress 1124 x_off = t->is_int()->get_con(); 1125 x = x->in(1); 1126 } 1127 1128 // Scan a right-spline-tree for MAXs 1129 Node* y = r; 1130 jint y_off = 0; 1131 // Check final part of MAX tree 1132 if (y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant 1133 y->in(2)->is_Con()) { 1134 const Type* t = y->in(2)->bottom_type(); 1135 if (t == Type::TOP) return NULL; // No progress 1136 y_off = t->is_int()->get_con(); 1137 y = y->in(1); 1138 } 1139 if (x->_idx > y->_idx && r->Opcode() != Op_MaxI) { 1140 swap_edges(1, 2); 1141 return this; 1142 } 1143 1144 const TypeInt* tx = phase->type(x)->isa_int(); 1145 1146 if (r->Opcode() == Op_MaxI) { 1147 assert(r != r->in(2), "dead loop in MaxINode::Ideal"); 1148 y = r->in(1); 1149 // Check final part of MAX tree 1150 if (y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant 1151 y->in(2)->is_Con()) { 1152 const Type* t = y->in(2)->bottom_type(); 1153 if (t == Type::TOP) return NULL; // No progress 1154 y_off = t->is_int()->get_con(); 1155 y = y->in(1); 1156 } 1157 1158 if (x->_idx > y->_idx) 1159 return new MaxINode(r->in(1), phase->transform(new MaxINode(l, r->in(2)))); 1160 1161 // Transform MAX2(x + c0, MAX2(x + c1, z)) into MAX2(x + MAX2(c0, c1), z) 1162 // if x == y and the additions can't overflow. 1163 if (x == y && tx != NULL && 1164 !can_overflow(tx, x_off) && 1165 !can_overflow(tx, y_off)) { 1166 return new MaxINode(phase->transform(new AddINode(x, phase->intcon(MAX2(x_off, y_off)))), r->in(2)); 1167 } 1168 } else { 1169 // Transform MAX2(x + c0, y + c1) into x + MAX2(c0, c1) 1170 // if x == y and the additions can't overflow. 1171 if (x == y && tx != NULL && 1172 !can_overflow(tx, x_off) && 1173 !can_overflow(tx, y_off)) { 1174 return new AddINode(x, phase->intcon(MAX2(x_off, y_off))); 1175 } 1176 } 1177 return NULL; 1178 } 1179 1180 //============================================================================= 1181 //------------------------------Idealize--------------------------------------- 1182 // MINs show up in range-check loop limit calculations. Look for 1183 // "MIN2(x+c0,MIN2(y,x+c1))". Pick the smaller constant: "MIN2(x+c0,y)" 1184 Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) { 1185 Node *progress = NULL; 1186 // Force a right-spline graph 1187 Node *l = in(1); 1188 Node *r = in(2); 1189 // Transform MinI1( MinI2(a,b), c) into MinI1( a, MinI2(b,c) ) 1190 // to force a right-spline graph for the rest of MinINode::Ideal(). 1191 if( l->Opcode() == Op_MinI ) { 1192 assert( l != l->in(1), "dead loop in MinINode::Ideal" ); 1193 r = phase->transform(new MinINode(l->in(2),r)); 1194 l = l->in(1); 1195 set_req_X(1, l, phase); 1196 set_req_X(2, r, phase); 1197 return this; 1198 } 1199 1200 // Get left input & constant 1201 Node *x = l; 1202 jint x_off = 0; 1203 if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant 1204 x->in(2)->is_Con() ) { 1205 const Type *t = x->in(2)->bottom_type(); 1206 if( t == Type::TOP ) return NULL; // No progress 1207 x_off = t->is_int()->get_con(); 1208 x = x->in(1); 1209 } 1210 1211 // Scan a right-spline-tree for MINs 1212 Node *y = r; 1213 jint y_off = 0; 1214 // Check final part of MIN tree 1215 if( y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant 1216 y->in(2)->is_Con() ) { 1217 const Type *t = y->in(2)->bottom_type(); 1218 if( t == Type::TOP ) return NULL; // No progress 1219 y_off = t->is_int()->get_con(); 1220 y = y->in(1); 1221 } 1222 if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) { 1223 swap_edges(1, 2); 1224 return this; 1225 } 1226 1227 const TypeInt* tx = phase->type(x)->isa_int(); 1228 1229 if( r->Opcode() == Op_MinI ) { 1230 assert( r != r->in(2), "dead loop in MinINode::Ideal" ); 1231 y = r->in(1); 1232 // Check final part of MIN tree 1233 if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant 1234 y->in(2)->is_Con() ) { 1235 const Type *t = y->in(2)->bottom_type(); 1236 if( t == Type::TOP ) return NULL; // No progress 1237 y_off = t->is_int()->get_con(); 1238 y = y->in(1); 1239 } 1240 1241 if( x->_idx > y->_idx ) 1242 return new MinINode(r->in(1),phase->transform(new MinINode(l,r->in(2)))); 1243 1244 // Transform MIN2(x + c0, MIN2(x + c1, z)) into MIN2(x + MIN2(c0, c1), z) 1245 // if x == y and the additions can't overflow. 1246 if (x == y && tx != NULL && 1247 !can_overflow(tx, x_off) && 1248 !can_overflow(tx, y_off)) { 1249 return new MinINode(phase->transform(new AddINode(x, phase->intcon(MIN2(x_off, y_off)))), r->in(2)); 1250 } 1251 } else { 1252 // Transform MIN2(x + c0, y + c1) into x + MIN2(c0, c1) 1253 // if x == y and the additions can't overflow. 1254 if (x == y && tx != NULL && 1255 !can_overflow(tx, x_off) && 1256 !can_overflow(tx, y_off)) { 1257 return new AddINode(x,phase->intcon(MIN2(x_off,y_off))); 1258 } 1259 } 1260 return NULL; 1261 } 1262 1263 //------------------------------add_ring--------------------------------------- 1264 // Supplied function returns the sum of the inputs. 1265 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const { 1266 const TypeInt *r0 = t0->is_int(); // Handy access 1267 const TypeInt *r1 = t1->is_int(); 1268 1269 // Otherwise just MIN them bits. 1270 return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) ); 1271 } 1272 1273 //------------------------------add_ring--------------------------------------- 1274 const Type *MinFNode::add_ring( const Type *t0, const Type *t1 ) const { 1275 const TypeF *r0 = t0->is_float_constant(); 1276 const TypeF *r1 = t1->is_float_constant(); 1277 1278 if (r0->is_nan()) { 1279 return r0; 1280 } 1281 if (r1->is_nan()) { 1282 return r1; 1283 } 1284 1285 float f0 = r0->getf(); 1286 float f1 = r1->getf(); 1287 if (f0 != 0.0f || f1 != 0.0f) { 1288 return f0 < f1 ? r0 : r1; 1289 } 1290 1291 // handle min of 0.0, -0.0 case. 1292 return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1; 1293 } 1294 1295 //------------------------------add_ring--------------------------------------- 1296 const Type *MinDNode::add_ring( const Type *t0, const Type *t1 ) const { 1297 const TypeD *r0 = t0->is_double_constant(); 1298 const TypeD *r1 = t1->is_double_constant(); 1299 1300 if (r0->is_nan()) { 1301 return r0; 1302 } 1303 if (r1->is_nan()) { 1304 return r1; 1305 } 1306 1307 double d0 = r0->getd(); 1308 double d1 = r1->getd(); 1309 if (d0 != 0.0 || d1 != 0.0) { 1310 return d0 < d1 ? r0 : r1; 1311 } 1312 1313 // handle min of 0.0, -0.0 case. 1314 return (jlong_cast(d0) < jlong_cast(d1)) ? r0 : r1; 1315 } 1316 1317 //------------------------------add_ring--------------------------------------- 1318 const Type *MaxFNode::add_ring( const Type *t0, const Type *t1 ) const { 1319 const TypeF *r0 = t0->is_float_constant(); 1320 const TypeF *r1 = t1->is_float_constant(); 1321 1322 if (r0->is_nan()) { 1323 return r0; 1324 } 1325 if (r1->is_nan()) { 1326 return r1; 1327 } 1328 1329 float f0 = r0->getf(); 1330 float f1 = r1->getf(); 1331 if (f0 != 0.0f || f1 != 0.0f) { 1332 return f0 > f1 ? r0 : r1; 1333 } 1334 1335 // handle max of 0.0,-0.0 case. 1336 return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1; 1337 } 1338 1339 //------------------------------add_ring--------------------------------------- 1340 const Type *MaxDNode::add_ring( const Type *t0, const Type *t1 ) const { 1341 const TypeD *r0 = t0->is_double_constant(); 1342 const TypeD *r1 = t1->is_double_constant(); 1343 1344 if (r0->is_nan()) { 1345 return r0; 1346 } 1347 if (r1->is_nan()) { 1348 return r1; 1349 } 1350 1351 double d0 = r0->getd(); 1352 double d1 = r1->getd(); 1353 if (d0 != 0.0 || d1 != 0.0) { 1354 return d0 > d1 ? r0 : r1; 1355 } 1356 1357 // handle max of 0.0, -0.0 case. 1358 return (jlong_cast(d0) > jlong_cast(d1)) ? r0 : r1; 1359 }