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