1 /* 2 * Copyright (c) 1997, 2010, 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/cfgnode.hpp" 29 #include "opto/connode.hpp" 30 #include "opto/machnode.hpp" 31 #include "opto/mulnode.hpp" 32 #include "opto/phaseX.hpp" 33 #include "opto/subnode.hpp" 34 35 // Portions of code courtesy of Clifford Click 36 37 #define MAXFLOAT ((float)3.40282346638528860e+38) 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( PhaseTransform *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( Node *add, int con_left, int con_right ) { 66 Node *in1 = add->in(1); 67 Node *in2 = add->in(2); 68 69 // Convert "1+x" into "x+1". 70 // Right is a constant; leave it 71 if( con_right ) return false; 72 // Left is a constant; move it right. 73 if( con_left ) { 74 add->swap_edges(1, 2); 75 return true; 76 } 77 78 // Convert "Load+x" into "x+Load". 79 // Now check for loads 80 if (in2->is_Load()) { 81 if (!in1->is_Load()) { 82 // already x+Load to return 83 return false; 84 } 85 // both are loads, so fall through to sort inputs by idx 86 } else if( in1->is_Load() ) { 87 // Left is a Load and Right is not; move it right. 88 add->swap_edges(1, 2); 89 return true; 90 } 91 92 PhiNode *phi; 93 // Check for tight loop increments: Loop-phi of Add of loop-phi 94 if( in1->is_Phi() && (phi = in1->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add) 95 return false; 96 if( in2->is_Phi() && (phi = in2->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add){ 97 add->swap_edges(1, 2); 98 return true; 99 } 100 101 // Otherwise, sort inputs (commutativity) to help value numbering. 102 if( in1->_idx > in2->_idx ) { 103 add->swap_edges(1, 2); 104 return true; 105 } 106 return false; 107 } 108 109 //------------------------------Idealize--------------------------------------- 110 // If we get here, we assume we are associative! 111 Node *AddNode::Ideal(PhaseGVN *phase, bool can_reshape) { 112 const Type *t1 = phase->type( in(1) ); 113 const Type *t2 = phase->type( in(2) ); 114 int con_left = t1->singleton(); 115 int con_right = t2->singleton(); 116 117 // Check for commutative operation desired 118 if( commute(this,con_left,con_right) ) return this; 119 120 AddNode *progress = NULL; // Progress flag 121 122 // Convert "(x+1)+2" into "x+(1+2)". If the right input is a 123 // constant, and the left input is an add of a constant, flatten the 124 // expression tree. 125 Node *add1 = in(1); 126 Node *add2 = in(2); 127 int add1_op = add1->Opcode(); 128 int this_op = Opcode(); 129 if( con_right && t2 != Type::TOP && // Right input is a constant? 130 add1_op == this_op ) { // Left input is an Add? 131 132 // Type of left _in right input 133 const Type *t12 = phase->type( add1->in(2) ); 134 if( t12->singleton() && t12 != Type::TOP ) { // Left input is an add of a constant? 135 // Check for rare case of closed data cycle which can happen inside 136 // unreachable loops. In these cases the computation is undefined. 137 #ifdef ASSERT 138 Node *add11 = add1->in(1); 139 int add11_op = add11->Opcode(); 140 if( (add1 == add1->in(1)) 141 || (add11_op == this_op && add11->in(1) == add1) ) { 142 assert(false, "dead loop in AddNode::Ideal"); 143 } 144 #endif 145 // The Add of the flattened expression 146 Node *x1 = add1->in(1); 147 Node *x2 = phase->makecon( add1->as_Add()->add_ring( t2, t12 )); 148 PhaseIterGVN *igvn = phase->is_IterGVN(); 149 if( igvn ) { 150 set_req_X(2,x2,igvn); 151 set_req_X(1,x1,igvn); 152 } else { 153 set_req(2,x2); 154 set_req(1,x1); 155 } 156 progress = this; // Made progress 157 add1 = in(1); 158 add1_op = add1->Opcode(); 159 } 160 } 161 162 // Convert "(x+1)+y" into "(x+y)+1". Push constants down the expression tree. 163 if( add1_op == this_op && !con_right ) { 164 Node *a12 = add1->in(2); 165 const Type *t12 = phase->type( a12 ); 166 if( t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) && 167 !(add1->in(1)->is_Phi() && add1->in(1)->as_Phi()->is_tripcount()) ) { 168 assert(add1->in(1) != this, "dead loop in AddNode::Ideal"); 169 add2 = add1->clone(); 170 add2->set_req(2, in(2)); 171 add2 = phase->transform(add2); 172 set_req(1, add2); 173 set_req(2, a12); 174 progress = this; 175 add2 = a12; 176 } 177 } 178 179 // Convert "x+(y+1)" into "(x+y)+1". Push constants down the expression tree. 180 int add2_op = add2->Opcode(); 181 if( add2_op == this_op && !con_left ) { 182 Node *a22 = add2->in(2); 183 const Type *t22 = phase->type( a22 ); 184 if( t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) && 185 !(add2->in(1)->is_Phi() && add2->in(1)->as_Phi()->is_tripcount()) ) { 186 assert(add2->in(1) != this, "dead loop in AddNode::Ideal"); 187 Node *addx = add2->clone(); 188 addx->set_req(1, in(1)); 189 addx->set_req(2, add2->in(1)); 190 addx = phase->transform(addx); 191 set_req(1, addx); 192 set_req(2, a22); 193 progress = this; 194 } 195 } 196 197 return progress; 198 } 199 200 //------------------------------Value----------------------------------------- 201 // An add node sums it's two _in. If one input is an RSD, we must mixin 202 // the other input's symbols. 203 const Type *AddNode::Value( PhaseTransform *phase ) const { 204 // Either input is TOP ==> the result is TOP 205 const Type *t1 = phase->type( in(1) ); 206 const Type *t2 = phase->type( in(2) ); 207 if( t1 == Type::TOP ) return Type::TOP; 208 if( t2 == Type::TOP ) return Type::TOP; 209 210 // Either input is BOTTOM ==> the result is the local BOTTOM 211 const Type *bot = bottom_type(); 212 if( (t1 == bot) || (t2 == bot) || 213 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 214 return bot; 215 216 // Check for an addition involving the additive identity 217 const Type *tadd = add_of_identity( t1, t2 ); 218 if( tadd ) return tadd; 219 220 return add_ring(t1,t2); // Local flavor of type addition 221 } 222 223 //------------------------------add_identity----------------------------------- 224 // Check for addition of the identity 225 const Type *AddNode::add_of_identity( const Type *t1, const Type *t2 ) const { 226 const Type *zero = add_id(); // The additive identity 227 if( t1->higher_equal( zero ) ) return t2; 228 if( t2->higher_equal( zero ) ) return t1; 229 230 return NULL; 231 } 232 233 234 //============================================================================= 235 //------------------------------Idealize--------------------------------------- 236 Node *AddINode::Ideal(PhaseGVN *phase, bool can_reshape) { 237 Node* in1 = in(1); 238 Node* in2 = in(2); 239 int op1 = in1->Opcode(); 240 int op2 = in2->Opcode(); 241 // Fold (con1-x)+con2 into (con1+con2)-x 242 if ( op1 == Op_AddI && op2 == Op_SubI ) { 243 // Swap edges to try optimizations below 244 in1 = in2; 245 in2 = in(1); 246 op1 = op2; 247 op2 = in2->Opcode(); 248 } 249 if( op1 == Op_SubI ) { 250 const Type *t_sub1 = phase->type( in1->in(1) ); 251 const Type *t_2 = phase->type( in2 ); 252 if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP ) 253 return new (phase->C, 3) SubINode(phase->makecon( add_ring( t_sub1, t_2 ) ), 254 in1->in(2) ); 255 // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)" 256 if( op2 == Op_SubI ) { 257 // Check for dead cycle: d = (a-b)+(c-d) 258 assert( in1->in(2) != this && in2->in(2) != this, 259 "dead loop in AddINode::Ideal" ); 260 Node *sub = new (phase->C, 3) SubINode(NULL, NULL); 261 sub->init_req(1, phase->transform(new (phase->C, 3) AddINode(in1->in(1), in2->in(1) ) )); 262 sub->init_req(2, phase->transform(new (phase->C, 3) AddINode(in1->in(2), in2->in(2) ) )); 263 return sub; 264 } 265 // Convert "(a-b)+(b+c)" into "(a+c)" 266 if( op2 == Op_AddI && in1->in(2) == in2->in(1) ) { 267 assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal"); 268 return new (phase->C, 3) AddINode(in1->in(1), in2->in(2)); 269 } 270 // Convert "(a-b)+(c+b)" into "(a+c)" 271 if( op2 == Op_AddI && in1->in(2) == in2->in(2) ) { 272 assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal"); 273 return new (phase->C, 3) AddINode(in1->in(1), in2->in(1)); 274 } 275 // Convert "(a-b)+(b-c)" into "(a-c)" 276 if( op2 == Op_SubI && in1->in(2) == in2->in(1) ) { 277 assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal"); 278 return new (phase->C, 3) SubINode(in1->in(1), in2->in(2)); 279 } 280 // Convert "(a-b)+(c-a)" into "(c-b)" 281 if( op2 == Op_SubI && in1->in(1) == in2->in(2) ) { 282 assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddINode::Ideal"); 283 return new (phase->C, 3) SubINode(in2->in(1), in1->in(2)); 284 } 285 } 286 287 // Convert "x+(0-y)" into "(x-y)" 288 if( op2 == Op_SubI && phase->type(in2->in(1)) == TypeInt::ZERO ) 289 return new (phase->C, 3) SubINode(in1, in2->in(2) ); 290 291 // Convert "(0-y)+x" into "(x-y)" 292 if( op1 == Op_SubI && phase->type(in1->in(1)) == TypeInt::ZERO ) 293 return new (phase->C, 3) SubINode( in2, in1->in(2) ); 294 295 // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y. 296 // Helps with array allocation math constant folding 297 // See 4790063: 298 // Unrestricted transformation is unsafe for some runtime values of 'x' 299 // ( x == 0, z == 1, y == -1 ) fails 300 // ( x == -5, z == 1, y == 1 ) fails 301 // Transform works for small z and small negative y when the addition 302 // (x + (y << z)) does not cross zero. 303 // Implement support for negative y and (x >= -(y << z)) 304 // Have not observed cases where type information exists to support 305 // positive y and (x <= -(y << z)) 306 if( op1 == Op_URShiftI && op2 == Op_ConI && 307 in1->in(2)->Opcode() == Op_ConI ) { 308 jint z = phase->type( in1->in(2) )->is_int()->get_con() & 0x1f; // only least significant 5 bits matter 309 jint y = phase->type( in2 )->is_int()->get_con(); 310 311 if( z < 5 && -5 < y && y < 0 ) { 312 const Type *t_in11 = phase->type(in1->in(1)); 313 if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z)) ) { 314 Node *a = phase->transform( new (phase->C, 3) AddINode( in1->in(1), phase->intcon(y<<z) ) ); 315 return new (phase->C, 3) URShiftINode( a, in1->in(2) ); 316 } 317 } 318 } 319 320 return AddNode::Ideal(phase, can_reshape); 321 } 322 323 324 //------------------------------Identity--------------------------------------- 325 // Fold (x-y)+y OR y+(x-y) into x 326 Node *AddINode::Identity( PhaseTransform *phase ) { 327 if( in(1)->Opcode() == Op_SubI && phase->eqv(in(1)->in(2),in(2)) ) { 328 return in(1)->in(1); 329 } 330 else if( in(2)->Opcode() == Op_SubI && phase->eqv(in(2)->in(2),in(1)) ) { 331 return in(2)->in(1); 332 } 333 return AddNode::Identity(phase); 334 } 335 336 337 //------------------------------add_ring--------------------------------------- 338 // Supplied function returns the sum of the inputs. Guaranteed never 339 // to be passed a TOP or BOTTOM type, these are filtered out by 340 // pre-check. 341 const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const { 342 const TypeInt *r0 = t0->is_int(); // Handy access 343 const TypeInt *r1 = t1->is_int(); 344 int lo = r0->_lo + r1->_lo; 345 int hi = r0->_hi + r1->_hi; 346 if( !(r0->is_con() && r1->is_con()) ) { 347 // Not both constants, compute approximate result 348 if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) { 349 lo = min_jint; hi = max_jint; // Underflow on the low side 350 } 351 if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) { 352 lo = min_jint; hi = max_jint; // Overflow on the high side 353 } 354 if( lo > hi ) { // Handle overflow 355 lo = min_jint; hi = max_jint; 356 } 357 } else { 358 // both constants, compute precise result using 'lo' and 'hi' 359 // Semantics define overflow and underflow for integer addition 360 // as expected. In particular: 0x80000000 + 0x80000000 --> 0x0 361 } 362 return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) ); 363 } 364 365 366 //============================================================================= 367 //------------------------------Idealize--------------------------------------- 368 Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) { 369 Node* in1 = in(1); 370 Node* in2 = in(2); 371 int op1 = in1->Opcode(); 372 int op2 = in2->Opcode(); 373 // Fold (con1-x)+con2 into (con1+con2)-x 374 if ( op1 == Op_AddL && op2 == Op_SubL ) { 375 // Swap edges to try optimizations below 376 in1 = in2; 377 in2 = in(1); 378 op1 = op2; 379 op2 = in2->Opcode(); 380 } 381 // Fold (con1-x)+con2 into (con1+con2)-x 382 if( op1 == Op_SubL ) { 383 const Type *t_sub1 = phase->type( in1->in(1) ); 384 const Type *t_2 = phase->type( in2 ); 385 if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP ) 386 return new (phase->C, 3) SubLNode(phase->makecon( add_ring( t_sub1, t_2 ) ), 387 in1->in(2) ); 388 // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)" 389 if( op2 == Op_SubL ) { 390 // Check for dead cycle: d = (a-b)+(c-d) 391 assert( in1->in(2) != this && in2->in(2) != this, 392 "dead loop in AddLNode::Ideal" ); 393 Node *sub = new (phase->C, 3) SubLNode(NULL, NULL); 394 sub->init_req(1, phase->transform(new (phase->C, 3) AddLNode(in1->in(1), in2->in(1) ) )); 395 sub->init_req(2, phase->transform(new (phase->C, 3) AddLNode(in1->in(2), in2->in(2) ) )); 396 return sub; 397 } 398 // Convert "(a-b)+(b+c)" into "(a+c)" 399 if( op2 == Op_AddL && in1->in(2) == in2->in(1) ) { 400 assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal"); 401 return new (phase->C, 3) AddLNode(in1->in(1), in2->in(2)); 402 } 403 // Convert "(a-b)+(c+b)" into "(a+c)" 404 if( op2 == Op_AddL && in1->in(2) == in2->in(2) ) { 405 assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal"); 406 return new (phase->C, 3) AddLNode(in1->in(1), in2->in(1)); 407 } 408 // Convert "(a-b)+(b-c)" into "(a-c)" 409 if( op2 == Op_SubL && in1->in(2) == in2->in(1) ) { 410 assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal"); 411 return new (phase->C, 3) SubLNode(in1->in(1), in2->in(2)); 412 } 413 // Convert "(a-b)+(c-a)" into "(c-b)" 414 if( op2 == Op_SubL && in1->in(1) == in1->in(2) ) { 415 assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal"); 416 return new (phase->C, 3) SubLNode(in2->in(1), in1->in(2)); 417 } 418 } 419 420 // Convert "x+(0-y)" into "(x-y)" 421 if( op2 == Op_SubL && phase->type(in2->in(1)) == TypeLong::ZERO ) 422 return new (phase->C, 3) SubLNode( in1, in2->in(2) ); 423 424 // Convert "(0-y)+x" into "(x-y)" 425 if( op1 == Op_SubL && phase->type(in1->in(1)) == TypeInt::ZERO ) 426 return new (phase->C, 3) SubLNode( in2, in1->in(2) ); 427 428 // Convert "X+X+X+X+X...+X+Y" into "k*X+Y" or really convert "X+(X+Y)" 429 // into "(X<<1)+Y" and let shift-folding happen. 430 if( op2 == Op_AddL && 431 in2->in(1) == in1 && 432 op1 != Op_ConL && 433 0 ) { 434 Node *shift = phase->transform(new (phase->C, 3) LShiftLNode(in1,phase->intcon(1))); 435 return new (phase->C, 3) AddLNode(shift,in2->in(2)); 436 } 437 438 return AddNode::Ideal(phase, can_reshape); 439 } 440 441 442 //------------------------------Identity--------------------------------------- 443 // Fold (x-y)+y OR y+(x-y) into x 444 Node *AddLNode::Identity( PhaseTransform *phase ) { 445 if( in(1)->Opcode() == Op_SubL && phase->eqv(in(1)->in(2),in(2)) ) { 446 return in(1)->in(1); 447 } 448 else if( in(2)->Opcode() == Op_SubL && phase->eqv(in(2)->in(2),in(1)) ) { 449 return in(2)->in(1); 450 } 451 return AddNode::Identity(phase); 452 } 453 454 455 //------------------------------add_ring--------------------------------------- 456 // Supplied function returns the sum of the inputs. Guaranteed never 457 // to be passed a TOP or BOTTOM type, these are filtered out by 458 // pre-check. 459 const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const { 460 const TypeLong *r0 = t0->is_long(); // Handy access 461 const TypeLong *r1 = t1->is_long(); 462 jlong lo = r0->_lo + r1->_lo; 463 jlong hi = r0->_hi + r1->_hi; 464 if( !(r0->is_con() && r1->is_con()) ) { 465 // Not both constants, compute approximate result 466 if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) { 467 lo =min_jlong; hi = max_jlong; // Underflow on the low side 468 } 469 if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) { 470 lo = min_jlong; hi = max_jlong; // Overflow on the high side 471 } 472 if( lo > hi ) { // Handle overflow 473 lo = min_jlong; hi = max_jlong; 474 } 475 } else { 476 // both constants, compute precise result using 'lo' and 'hi' 477 // Semantics define overflow and underflow for integer addition 478 // as expected. In particular: 0x80000000 + 0x80000000 --> 0x0 479 } 480 return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) ); 481 } 482 483 484 //============================================================================= 485 //------------------------------add_of_identity-------------------------------- 486 // Check for addition of the identity 487 const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const { 488 // x ADD 0 should return x unless 'x' is a -zero 489 // 490 // const Type *zero = add_id(); // The additive identity 491 // jfloat f1 = t1->getf(); 492 // jfloat f2 = t2->getf(); 493 // 494 // if( t1->higher_equal( zero ) ) return t2; 495 // if( t2->higher_equal( zero ) ) return t1; 496 497 return NULL; 498 } 499 500 //------------------------------add_ring--------------------------------------- 501 // Supplied function returns the sum of the inputs. 502 // This also type-checks the inputs for sanity. Guaranteed never to 503 // be passed a TOP or BOTTOM type, these are filtered out by pre-check. 504 const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const { 505 // We must be adding 2 float constants. 506 return TypeF::make( t0->getf() + t1->getf() ); 507 } 508 509 //------------------------------Ideal------------------------------------------ 510 Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) { 511 if( IdealizedNumerics && !phase->C->method()->is_strict() ) { 512 return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms 513 } 514 515 // Floating point additions are not associative because of boundary conditions (infinity) 516 return commute(this, 517 phase->type( in(1) )->singleton(), 518 phase->type( in(2) )->singleton() ) ? this : NULL; 519 } 520 521 522 //============================================================================= 523 //------------------------------add_of_identity-------------------------------- 524 // Check for addition of the identity 525 const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const { 526 // x ADD 0 should return x unless 'x' is a -zero 527 // 528 // const Type *zero = add_id(); // The additive identity 529 // jfloat f1 = t1->getf(); 530 // jfloat f2 = t2->getf(); 531 // 532 // if( t1->higher_equal( zero ) ) return t2; 533 // if( t2->higher_equal( zero ) ) return t1; 534 535 return NULL; 536 } 537 //------------------------------add_ring--------------------------------------- 538 // Supplied function returns the sum of the inputs. 539 // This also type-checks the inputs for sanity. Guaranteed never to 540 // be passed a TOP or BOTTOM type, these are filtered out by pre-check. 541 const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const { 542 // We must be adding 2 double constants. 543 return TypeD::make( t0->getd() + t1->getd() ); 544 } 545 546 //------------------------------Ideal------------------------------------------ 547 Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) { 548 if( IdealizedNumerics && !phase->C->method()->is_strict() ) { 549 return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms 550 } 551 552 // Floating point additions are not associative because of boundary conditions (infinity) 553 return commute(this, 554 phase->type( in(1) )->singleton(), 555 phase->type( in(2) )->singleton() ) ? this : NULL; 556 } 557 558 559 //============================================================================= 560 //------------------------------Identity--------------------------------------- 561 // If one input is a constant 0, return the other input. 562 Node *AddPNode::Identity( PhaseTransform *phase ) { 563 return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this; 564 } 565 566 //------------------------------Idealize--------------------------------------- 567 Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) { 568 // Bail out if dead inputs 569 if( phase->type( in(Address) ) == Type::TOP ) return NULL; 570 571 // If the left input is an add of a constant, flatten the expression tree. 572 const Node *n = in(Address); 573 if (n->is_AddP() && n->in(Base) == in(Base)) { 574 const AddPNode *addp = n->as_AddP(); // Left input is an AddP 575 assert( !addp->in(Address)->is_AddP() || 576 addp->in(Address)->as_AddP() != addp, 577 "dead loop in AddPNode::Ideal" ); 578 // Type of left input's right input 579 const Type *t = phase->type( addp->in(Offset) ); 580 if( t == Type::TOP ) return NULL; 581 const TypeX *t12 = t->is_intptr_t(); 582 if( t12->is_con() ) { // Left input is an add of a constant? 583 // If the right input is a constant, combine constants 584 const Type *temp_t2 = phase->type( in(Offset) ); 585 if( temp_t2 == Type::TOP ) return NULL; 586 const TypeX *t2 = temp_t2->is_intptr_t(); 587 Node* address; 588 Node* offset; 589 if( t2->is_con() ) { 590 // The Add of the flattened expression 591 address = addp->in(Address); 592 offset = phase->MakeConX(t2->get_con() + t12->get_con()); 593 } else { 594 // Else move the constant to the right. ((A+con)+B) into ((A+B)+con) 595 address = phase->transform(new (phase->C, 4) AddPNode(in(Base),addp->in(Address),in(Offset))); 596 offset = addp->in(Offset); 597 } 598 PhaseIterGVN *igvn = phase->is_IterGVN(); 599 if( igvn ) { 600 set_req_X(Address,address,igvn); 601 set_req_X(Offset,offset,igvn); 602 } else { 603 set_req(Address,address); 604 set_req(Offset,offset); 605 } 606 return this; 607 } 608 } 609 610 // Raw pointers? 611 if( in(Base)->bottom_type() == Type::TOP ) { 612 // If this is a NULL+long form (from unsafe accesses), switch to a rawptr. 613 if (phase->type(in(Address)) == TypePtr::NULL_PTR) { 614 Node* offset = in(Offset); 615 return new (phase->C, 2) CastX2PNode(offset); 616 } 617 } 618 619 // If the right is an add of a constant, push the offset down. 620 // Convert: (ptr + (offset+con)) into (ptr+offset)+con. 621 // The idea is to merge array_base+scaled_index groups together, 622 // and only have different constant offsets from the same base. 623 const Node *add = in(Offset); 624 if( add->Opcode() == Op_AddX && add->in(1) != add ) { 625 const Type *t22 = phase->type( add->in(2) ); 626 if( t22->singleton() && (t22 != Type::TOP) ) { // Right input is an add of a constant? 627 set_req(Address, phase->transform(new (phase->C, 4) AddPNode(in(Base),in(Address),add->in(1)))); 628 set_req(Offset, add->in(2)); 629 return this; // Made progress 630 } 631 } 632 633 return NULL; // No progress 634 } 635 636 //------------------------------bottom_type------------------------------------ 637 // Bottom-type is the pointer-type with unknown offset. 638 const Type *AddPNode::bottom_type() const { 639 if (in(Address) == NULL) return TypePtr::BOTTOM; 640 const TypePtr *tp = in(Address)->bottom_type()->isa_ptr(); 641 if( !tp ) return Type::TOP; // TOP input means TOP output 642 assert( in(Offset)->Opcode() != Op_ConP, "" ); 643 const Type *t = in(Offset)->bottom_type(); 644 if( t == Type::TOP ) 645 return tp->add_offset(Type::OffsetTop); 646 const TypeX *tx = t->is_intptr_t(); 647 intptr_t txoffset = Type::OffsetBot; 648 if (tx->is_con()) { // Left input is an add of a constant? 649 txoffset = tx->get_con(); 650 } 651 return tp->add_offset(txoffset); 652 } 653 654 //------------------------------Value------------------------------------------ 655 const Type *AddPNode::Value( PhaseTransform *phase ) const { 656 // Either input is TOP ==> the result is TOP 657 const Type *t1 = phase->type( in(Address) ); 658 const Type *t2 = phase->type( in(Offset) ); 659 if( t1 == Type::TOP ) return Type::TOP; 660 if( t2 == Type::TOP ) return Type::TOP; 661 662 // Left input is a pointer 663 const TypePtr *p1 = t1->isa_ptr(); 664 // Right input is an int 665 const TypeX *p2 = t2->is_intptr_t(); 666 // Add 'em 667 intptr_t p2offset = Type::OffsetBot; 668 if (p2->is_con()) { // Left input is an add of a constant? 669 p2offset = p2->get_con(); 670 } 671 return p1->add_offset(p2offset); 672 } 673 674 //------------------------Ideal_base_and_offset-------------------------------- 675 // Split an oop pointer into a base and offset. 676 // (The offset might be Type::OffsetBot in the case of an array.) 677 // Return the base, or NULL if failure. 678 Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseTransform* phase, 679 // second return value: 680 intptr_t& offset) { 681 if (ptr->is_AddP()) { 682 Node* base = ptr->in(AddPNode::Base); 683 Node* addr = ptr->in(AddPNode::Address); 684 Node* offs = ptr->in(AddPNode::Offset); 685 if (base == addr || base->is_top()) { 686 offset = phase->find_intptr_t_con(offs, Type::OffsetBot); 687 if (offset != Type::OffsetBot) { 688 return addr; 689 } 690 } 691 } 692 offset = Type::OffsetBot; 693 return NULL; 694 } 695 696 //------------------------------unpack_offsets---------------------------------- 697 // Collect the AddP offset values into the elements array, giving up 698 // if there are more than length. 699 int AddPNode::unpack_offsets(Node* elements[], int length) { 700 int count = 0; 701 Node* addr = this; 702 Node* base = addr->in(AddPNode::Base); 703 while (addr->is_AddP()) { 704 if (addr->in(AddPNode::Base) != base) { 705 // give up 706 return -1; 707 } 708 elements[count++] = addr->in(AddPNode::Offset); 709 if (count == length) { 710 // give up 711 return -1; 712 } 713 addr = addr->in(AddPNode::Address); 714 } 715 if (addr != base) { 716 return -1; 717 } 718 return count; 719 } 720 721 //------------------------------match_edge------------------------------------- 722 // Do we Match on this edge index or not? Do not match base pointer edge 723 uint AddPNode::match_edge(uint idx) const { 724 return idx > Base; 725 } 726 727 //============================================================================= 728 //------------------------------Identity--------------------------------------- 729 Node *OrINode::Identity( PhaseTransform *phase ) { 730 // x | x => x 731 if (phase->eqv(in(1), in(2))) { 732 return in(1); 733 } 734 735 return AddNode::Identity(phase); 736 } 737 738 //------------------------------add_ring--------------------------------------- 739 // Supplied function returns the sum of the inputs IN THE CURRENT RING. For 740 // the logical operations the ring's ADD is really a logical OR function. 741 // This also type-checks the inputs for sanity. Guaranteed never to 742 // be passed a TOP or BOTTOM type, these are filtered out by pre-check. 743 const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const { 744 const TypeInt *r0 = t0->is_int(); // Handy access 745 const TypeInt *r1 = t1->is_int(); 746 747 // If both args are bool, can figure out better types 748 if ( r0 == TypeInt::BOOL ) { 749 if ( r1 == TypeInt::ONE) { 750 return TypeInt::ONE; 751 } else if ( r1 == TypeInt::BOOL ) { 752 return TypeInt::BOOL; 753 } 754 } else if ( r0 == TypeInt::ONE ) { 755 if ( r1 == TypeInt::BOOL ) { 756 return TypeInt::ONE; 757 } 758 } 759 760 // If either input is not a constant, just return all integers. 761 if( !r0->is_con() || !r1->is_con() ) 762 return TypeInt::INT; // Any integer, but still no symbols. 763 764 // Otherwise just OR them bits. 765 return TypeInt::make( r0->get_con() | r1->get_con() ); 766 } 767 768 //============================================================================= 769 //------------------------------Identity--------------------------------------- 770 Node *OrLNode::Identity( PhaseTransform *phase ) { 771 // x | x => x 772 if (phase->eqv(in(1), in(2))) { 773 return in(1); 774 } 775 776 return AddNode::Identity(phase); 777 } 778 779 //------------------------------add_ring--------------------------------------- 780 const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const { 781 const TypeLong *r0 = t0->is_long(); // Handy access 782 const TypeLong *r1 = t1->is_long(); 783 784 // If either input is not a constant, just return all integers. 785 if( !r0->is_con() || !r1->is_con() ) 786 return TypeLong::LONG; // Any integer, but still no symbols. 787 788 // Otherwise just OR them bits. 789 return TypeLong::make( r0->get_con() | r1->get_con() ); 790 } 791 792 //============================================================================= 793 //------------------------------add_ring--------------------------------------- 794 // Supplied function returns the sum of the inputs IN THE CURRENT RING. For 795 // the logical operations the ring's ADD is really a logical OR function. 796 // This also type-checks the inputs for sanity. Guaranteed never to 797 // be passed a TOP or BOTTOM type, these are filtered out by pre-check. 798 const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const { 799 const TypeInt *r0 = t0->is_int(); // Handy access 800 const TypeInt *r1 = t1->is_int(); 801 802 // Complementing a boolean? 803 if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE 804 || r1 == TypeInt::BOOL)) 805 return TypeInt::BOOL; 806 807 if( !r0->is_con() || !r1->is_con() ) // Not constants 808 return TypeInt::INT; // Any integer, but still no symbols. 809 810 // Otherwise just XOR them bits. 811 return TypeInt::make( r0->get_con() ^ r1->get_con() ); 812 } 813 814 //============================================================================= 815 //------------------------------add_ring--------------------------------------- 816 const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const { 817 const TypeLong *r0 = t0->is_long(); // Handy access 818 const TypeLong *r1 = t1->is_long(); 819 820 // If either input is not a constant, just return all integers. 821 if( !r0->is_con() || !r1->is_con() ) 822 return TypeLong::LONG; // Any integer, but still no symbols. 823 824 // Otherwise just OR them bits. 825 return TypeLong::make( r0->get_con() ^ r1->get_con() ); 826 } 827 828 //============================================================================= 829 //------------------------------add_ring--------------------------------------- 830 // Supplied function returns the sum of the inputs. 831 const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const { 832 const TypeInt *r0 = t0->is_int(); // Handy access 833 const TypeInt *r1 = t1->is_int(); 834 835 // Otherwise just MAX them bits. 836 return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) ); 837 } 838 839 //============================================================================= 840 //------------------------------Idealize--------------------------------------- 841 // MINs show up in range-check loop limit calculations. Look for 842 // "MIN2(x+c0,MIN2(y,x+c1))". Pick the smaller constant: "MIN2(x+c0,y)" 843 Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) { 844 Node *progress = NULL; 845 // Force a right-spline graph 846 Node *l = in(1); 847 Node *r = in(2); 848 // Transform MinI1( MinI2(a,b), c) into MinI1( a, MinI2(b,c) ) 849 // to force a right-spline graph for the rest of MinINode::Ideal(). 850 if( l->Opcode() == Op_MinI ) { 851 assert( l != l->in(1), "dead loop in MinINode::Ideal" ); 852 r = phase->transform(new (phase->C, 3) MinINode(l->in(2),r)); 853 l = l->in(1); 854 set_req(1, l); 855 set_req(2, r); 856 return this; 857 } 858 859 // Get left input & constant 860 Node *x = l; 861 int x_off = 0; 862 if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant 863 x->in(2)->is_Con() ) { 864 const Type *t = x->in(2)->bottom_type(); 865 if( t == Type::TOP ) return NULL; // No progress 866 x_off = t->is_int()->get_con(); 867 x = x->in(1); 868 } 869 870 // Scan a right-spline-tree for MINs 871 Node *y = r; 872 int y_off = 0; 873 // Check final part of MIN tree 874 if( y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant 875 y->in(2)->is_Con() ) { 876 const Type *t = y->in(2)->bottom_type(); 877 if( t == Type::TOP ) return NULL; // No progress 878 y_off = t->is_int()->get_con(); 879 y = y->in(1); 880 } 881 if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) { 882 swap_edges(1, 2); 883 return this; 884 } 885 886 887 if( r->Opcode() == Op_MinI ) { 888 assert( r != r->in(2), "dead loop in MinINode::Ideal" ); 889 y = r->in(1); 890 // Check final part of MIN tree 891 if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant 892 y->in(2)->is_Con() ) { 893 const Type *t = y->in(2)->bottom_type(); 894 if( t == Type::TOP ) return NULL; // No progress 895 y_off = t->is_int()->get_con(); 896 y = y->in(1); 897 } 898 899 if( x->_idx > y->_idx ) 900 return new (phase->C, 3) MinINode(r->in(1),phase->transform(new (phase->C, 3) MinINode(l,r->in(2)))); 901 902 // See if covers: MIN2(x+c0,MIN2(y+c1,z)) 903 if( !phase->eqv(x,y) ) return NULL; 904 // If (y == x) transform MIN2(x+c0, MIN2(x+c1,z)) into 905 // MIN2(x+c0 or x+c1 which less, z). 906 return new (phase->C, 3) MinINode(phase->transform(new (phase->C, 3) AddINode(x,phase->intcon(MIN2(x_off,y_off)))),r->in(2)); 907 } else { 908 // See if covers: MIN2(x+c0,y+c1) 909 if( !phase->eqv(x,y) ) return NULL; 910 // If (y == x) transform MIN2(x+c0,x+c1) into x+c0 or x+c1 which less. 911 return new (phase->C, 3) AddINode(x,phase->intcon(MIN2(x_off,y_off))); 912 } 913 914 } 915 916 //------------------------------add_ring--------------------------------------- 917 // Supplied function returns the sum of the inputs. 918 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const { 919 const TypeInt *r0 = t0->is_int(); // Handy access 920 const TypeInt *r1 = t1->is_int(); 921 922 // Otherwise just MIN them bits. 923 return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) ); 924 }