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