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/compile.hpp" 29 #include "opto/connode.hpp" 30 #include "opto/machnode.hpp" 31 #include "opto/matcher.hpp" 32 #include "opto/memnode.hpp" 33 #include "opto/phaseX.hpp" 34 #include "opto/subnode.hpp" 35 #include "runtime/sharedRuntime.hpp" 36 37 // Optimization - Graph Style 38 39 //============================================================================= 40 //------------------------------hash------------------------------------------- 41 uint ConNode::hash() const { 42 return (uintptr_t)in(TypeFunc::Control) + _type->hash(); 43 } 44 45 //------------------------------make------------------------------------------- 46 ConNode *ConNode::make( Compile* C, const Type *t ) { 47 switch( t->basic_type() ) { 48 case T_INT: return new (C, 1) ConINode( t->is_int() ); 49 case T_LONG: return new (C, 1) ConLNode( t->is_long() ); 50 case T_FLOAT: return new (C, 1) ConFNode( t->is_float_constant() ); 51 case T_DOUBLE: return new (C, 1) ConDNode( t->is_double_constant() ); 52 case T_VOID: return new (C, 1) ConNode ( Type::TOP ); 53 case T_OBJECT: return new (C, 1) ConPNode( t->is_oopptr() ); 54 case T_ARRAY: return new (C, 1) ConPNode( t->is_aryptr() ); 55 case T_ADDRESS: return new (C, 1) ConPNode( t->is_ptr() ); 56 case T_NARROWOOP: return new (C, 1) ConNNode( t->is_narrowoop() ); 57 // Expected cases: TypePtr::NULL_PTR, any is_rawptr() 58 // Also seen: AnyPtr(TopPTR *+top); from command line: 59 // r -XX:+PrintOpto -XX:CIStart=285 -XX:+CompileTheWorld -XX:CompileTheWorldStartAt=660 60 // %%%% Stop using TypePtr::NULL_PTR to represent nulls: use either TypeRawPtr::NULL_PTR 61 // or else TypeOopPtr::NULL_PTR. Then set Type::_basic_type[AnyPtr] = T_ILLEGAL 62 } 63 ShouldNotReachHere(); 64 return NULL; 65 } 66 67 //============================================================================= 68 /* 69 The major change is for CMoveP and StrComp. They have related but slightly 70 different problems. They both take in TWO oops which are both null-checked 71 independently before the using Node. After CCP removes the CastPP's they need 72 to pick up the guarding test edge - in this case TWO control edges. I tried 73 various solutions, all have problems: 74 75 (1) Do nothing. This leads to a bug where we hoist a Load from a CMoveP or a 76 StrComp above a guarding null check. I've seen both cases in normal -Xcomp 77 testing. 78 79 (2) Plug the control edge from 1 of the 2 oops in. Apparent problem here is 80 to figure out which test post-dominates. The real problem is that it doesn't 81 matter which one you pick. After you pick up, the dominating-test elider in 82 IGVN can remove the test and allow you to hoist up to the dominating test on 83 the chosen oop bypassing the test on the not-chosen oop. Seen in testing. 84 Oops. 85 86 (3) Leave the CastPP's in. This makes the graph more accurate in some sense; 87 we get to keep around the knowledge that an oop is not-null after some test. 88 Alas, the CastPP's interfere with GVN (some values are the regular oop, some 89 are the CastPP of the oop, all merge at Phi's which cannot collapse, etc). 90 This cost us 10% on SpecJVM, even when I removed some of the more trivial 91 cases in the optimizer. Removing more useless Phi's started allowing Loads to 92 illegally float above null checks. I gave up on this approach. 93 94 (4) Add BOTH control edges to both tests. Alas, too much code knows that 95 control edges are in slot-zero ONLY. Many quick asserts fail; no way to do 96 this one. Note that I really want to allow the CMoveP to float and add both 97 control edges to the dependent Load op - meaning I can select early but I 98 cannot Load until I pass both tests. 99 100 (5) Do not hoist CMoveP and StrComp. To this end I added the v-call 101 depends_only_on_test(). No obvious performance loss on Spec, but we are 102 clearly conservative on CMoveP (also so on StrComp but that's unlikely to 103 matter ever). 104 105 */ 106 107 108 //------------------------------Ideal------------------------------------------ 109 // Return a node which is more "ideal" than the current node. 110 // Move constants to the right. 111 Node *CMoveNode::Ideal(PhaseGVN *phase, bool can_reshape) { 112 if( in(0) && remove_dead_region(phase, can_reshape) ) return this; 113 // Don't bother trying to transform a dead node 114 if( in(0) && in(0)->is_top() ) return NULL; 115 assert( !phase->eqv(in(Condition), this) && 116 !phase->eqv(in(IfFalse), this) && 117 !phase->eqv(in(IfTrue), this), "dead loop in CMoveNode::Ideal" ); 118 if( phase->type(in(Condition)) == Type::TOP ) 119 return NULL; // return NULL when Condition is dead 120 121 if( in(IfFalse)->is_Con() && !in(IfTrue)->is_Con() ) { 122 if( in(Condition)->is_Bool() ) { 123 BoolNode* b = in(Condition)->as_Bool(); 124 BoolNode* b2 = b->negate(phase); 125 return make( phase->C, in(Control), phase->transform(b2), in(IfTrue), in(IfFalse), _type ); 126 } 127 } 128 return NULL; 129 } 130 131 //------------------------------is_cmove_id------------------------------------ 132 // Helper function to check for CMOVE identity. Shared with PhiNode::Identity 133 Node *CMoveNode::is_cmove_id( PhaseTransform *phase, Node *cmp, Node *t, Node *f, BoolNode *b ) { 134 // Check for Cmp'ing and CMove'ing same values 135 if( (phase->eqv(cmp->in(1),f) && 136 phase->eqv(cmp->in(2),t)) || 137 // Swapped Cmp is OK 138 (phase->eqv(cmp->in(2),f) && 139 phase->eqv(cmp->in(1),t)) ) { 140 // Give up this identity check for floating points because it may choose incorrect 141 // value around 0.0 and -0.0 142 if ( cmp->Opcode()==Op_CmpF || cmp->Opcode()==Op_CmpD ) 143 return NULL; 144 // Check for "(t==f)?t:f;" and replace with "f" 145 if( b->_test._test == BoolTest::eq ) 146 return f; 147 // Allow the inverted case as well 148 // Check for "(t!=f)?t:f;" and replace with "t" 149 if( b->_test._test == BoolTest::ne ) 150 return t; 151 } 152 return NULL; 153 } 154 155 //------------------------------Identity--------------------------------------- 156 // Conditional-move is an identity if both inputs are the same, or the test 157 // true or false. 158 Node *CMoveNode::Identity( PhaseTransform *phase ) { 159 if( phase->eqv(in(IfFalse),in(IfTrue)) ) // C-moving identical inputs? 160 return in(IfFalse); // Then it doesn't matter 161 if( phase->type(in(Condition)) == TypeInt::ZERO ) 162 return in(IfFalse); // Always pick left(false) input 163 if( phase->type(in(Condition)) == TypeInt::ONE ) 164 return in(IfTrue); // Always pick right(true) input 165 166 // Check for CMove'ing a constant after comparing against the constant. 167 // Happens all the time now, since if we compare equality vs a constant in 168 // the parser, we "know" the variable is constant on one path and we force 169 // it. Thus code like "if( x==0 ) {/*EMPTY*/}" ends up inserting a 170 // conditional move: "x = (x==0)?0:x;". Yucko. This fix is slightly more 171 // general in that we don't need constants. 172 if( in(Condition)->is_Bool() ) { 173 BoolNode *b = in(Condition)->as_Bool(); 174 Node *cmp = b->in(1); 175 if( cmp->is_Cmp() ) { 176 Node *id = is_cmove_id( phase, cmp, in(IfTrue), in(IfFalse), b ); 177 if( id ) return id; 178 } 179 } 180 181 return this; 182 } 183 184 //------------------------------Value------------------------------------------ 185 // Result is the meet of inputs 186 const Type *CMoveNode::Value( PhaseTransform *phase ) const { 187 if( phase->type(in(Condition)) == Type::TOP ) 188 return Type::TOP; 189 return phase->type(in(IfFalse))->meet(phase->type(in(IfTrue))); 190 } 191 192 //------------------------------make------------------------------------------- 193 // Make a correctly-flavored CMove. Since _type is directly determined 194 // from the inputs we do not need to specify it here. 195 CMoveNode *CMoveNode::make( Compile *C, Node *c, Node *bol, Node *left, Node *right, const Type *t ) { 196 switch( t->basic_type() ) { 197 case T_INT: return new (C, 4) CMoveINode( bol, left, right, t->is_int() ); 198 case T_FLOAT: return new (C, 4) CMoveFNode( bol, left, right, t ); 199 case T_DOUBLE: return new (C, 4) CMoveDNode( bol, left, right, t ); 200 case T_LONG: return new (C, 4) CMoveLNode( bol, left, right, t->is_long() ); 201 case T_OBJECT: return new (C, 4) CMovePNode( c, bol, left, right, t->is_oopptr() ); 202 case T_ADDRESS: return new (C, 4) CMovePNode( c, bol, left, right, t->is_ptr() ); 203 case T_NARROWOOP: return new (C, 4) CMoveNNode( c, bol, left, right, t ); 204 default: 205 ShouldNotReachHere(); 206 return NULL; 207 } 208 } 209 210 //============================================================================= 211 //------------------------------Ideal------------------------------------------ 212 // Return a node which is more "ideal" than the current node. 213 // Check for conversions to boolean 214 Node *CMoveINode::Ideal(PhaseGVN *phase, bool can_reshape) { 215 // Try generic ideal's first 216 Node *x = CMoveNode::Ideal(phase, can_reshape); 217 if( x ) return x; 218 219 // If zero is on the left (false-case, no-move-case) it must mean another 220 // constant is on the right (otherwise the shared CMove::Ideal code would 221 // have moved the constant to the right). This situation is bad for Intel 222 // and a don't-care for Sparc. It's bad for Intel because the zero has to 223 // be manifested in a register with a XOR which kills flags, which are live 224 // on input to the CMoveI, leading to a situation which causes excessive 225 // spilling on Intel. For Sparc, if the zero in on the left the Sparc will 226 // zero a register via G0 and conditionally-move the other constant. If the 227 // zero is on the right, the Sparc will load the first constant with a 228 // 13-bit set-lo and conditionally move G0. See bug 4677505. 229 if( phase->type(in(IfFalse)) == TypeInt::ZERO && !(phase->type(in(IfTrue)) == TypeInt::ZERO) ) { 230 if( in(Condition)->is_Bool() ) { 231 BoolNode* b = in(Condition)->as_Bool(); 232 BoolNode* b2 = b->negate(phase); 233 return make( phase->C, in(Control), phase->transform(b2), in(IfTrue), in(IfFalse), _type ); 234 } 235 } 236 237 // Now check for booleans 238 int flip = 0; 239 240 // Check for picking from zero/one 241 if( phase->type(in(IfFalse)) == TypeInt::ZERO && phase->type(in(IfTrue)) == TypeInt::ONE ) { 242 flip = 1 - flip; 243 } else if( phase->type(in(IfFalse)) == TypeInt::ONE && phase->type(in(IfTrue)) == TypeInt::ZERO ) { 244 } else return NULL; 245 246 // Check for eq/ne test 247 if( !in(1)->is_Bool() ) return NULL; 248 BoolNode *bol = in(1)->as_Bool(); 249 if( bol->_test._test == BoolTest::eq ) { 250 } else if( bol->_test._test == BoolTest::ne ) { 251 flip = 1-flip; 252 } else return NULL; 253 254 // Check for vs 0 or 1 255 if( !bol->in(1)->is_Cmp() ) return NULL; 256 const CmpNode *cmp = bol->in(1)->as_Cmp(); 257 if( phase->type(cmp->in(2)) == TypeInt::ZERO ) { 258 } else if( phase->type(cmp->in(2)) == TypeInt::ONE ) { 259 // Allow cmp-vs-1 if the other input is bounded by 0-1 260 if( phase->type(cmp->in(1)) != TypeInt::BOOL ) 261 return NULL; 262 flip = 1 - flip; 263 } else return NULL; 264 265 // Convert to a bool (flipped) 266 // Build int->bool conversion 267 #ifndef PRODUCT 268 if( PrintOpto ) tty->print_cr("CMOV to I2B"); 269 #endif 270 Node *n = new (phase->C, 2) Conv2BNode( cmp->in(1) ); 271 if( flip ) 272 n = new (phase->C, 3) XorINode( phase->transform(n), phase->intcon(1) ); 273 274 return n; 275 } 276 277 //============================================================================= 278 //------------------------------Ideal------------------------------------------ 279 // Return a node which is more "ideal" than the current node. 280 // Check for absolute value 281 Node *CMoveFNode::Ideal(PhaseGVN *phase, bool can_reshape) { 282 // Try generic ideal's first 283 Node *x = CMoveNode::Ideal(phase, can_reshape); 284 if( x ) return x; 285 286 int cmp_zero_idx = 0; // Index of compare input where to look for zero 287 int phi_x_idx = 0; // Index of phi input where to find naked x 288 289 // Find the Bool 290 if( !in(1)->is_Bool() ) return NULL; 291 BoolNode *bol = in(1)->as_Bool(); 292 // Check bool sense 293 switch( bol->_test._test ) { 294 case BoolTest::lt: cmp_zero_idx = 1; phi_x_idx = IfTrue; break; 295 case BoolTest::le: cmp_zero_idx = 2; phi_x_idx = IfFalse; break; 296 case BoolTest::gt: cmp_zero_idx = 2; phi_x_idx = IfTrue; break; 297 case BoolTest::ge: cmp_zero_idx = 1; phi_x_idx = IfFalse; break; 298 default: return NULL; break; 299 } 300 301 // Find zero input of CmpF; the other input is being abs'd 302 Node *cmpf = bol->in(1); 303 if( cmpf->Opcode() != Op_CmpF ) return NULL; 304 Node *X = NULL; 305 bool flip = false; 306 if( phase->type(cmpf->in(cmp_zero_idx)) == TypeF::ZERO ) { 307 X = cmpf->in(3 - cmp_zero_idx); 308 } else if (phase->type(cmpf->in(3 - cmp_zero_idx)) == TypeF::ZERO) { 309 // The test is inverted, we should invert the result... 310 X = cmpf->in(cmp_zero_idx); 311 flip = true; 312 } else { 313 return NULL; 314 } 315 316 // If X is found on the appropriate phi input, find the subtract on the other 317 if( X != in(phi_x_idx) ) return NULL; 318 int phi_sub_idx = phi_x_idx == IfTrue ? IfFalse : IfTrue; 319 Node *sub = in(phi_sub_idx); 320 321 // Allow only SubF(0,X) and fail out for all others; NegF is not OK 322 if( sub->Opcode() != Op_SubF || 323 sub->in(2) != X || 324 phase->type(sub->in(1)) != TypeF::ZERO ) return NULL; 325 326 Node *abs = new (phase->C, 2) AbsFNode( X ); 327 if( flip ) 328 abs = new (phase->C, 3) SubFNode(sub->in(1), phase->transform(abs)); 329 330 return abs; 331 } 332 333 //============================================================================= 334 //------------------------------Ideal------------------------------------------ 335 // Return a node which is more "ideal" than the current node. 336 // Check for absolute value 337 Node *CMoveDNode::Ideal(PhaseGVN *phase, bool can_reshape) { 338 // Try generic ideal's first 339 Node *x = CMoveNode::Ideal(phase, can_reshape); 340 if( x ) return x; 341 342 int cmp_zero_idx = 0; // Index of compare input where to look for zero 343 int phi_x_idx = 0; // Index of phi input where to find naked x 344 345 // Find the Bool 346 if( !in(1)->is_Bool() ) return NULL; 347 BoolNode *bol = in(1)->as_Bool(); 348 // Check bool sense 349 switch( bol->_test._test ) { 350 case BoolTest::lt: cmp_zero_idx = 1; phi_x_idx = IfTrue; break; 351 case BoolTest::le: cmp_zero_idx = 2; phi_x_idx = IfFalse; break; 352 case BoolTest::gt: cmp_zero_idx = 2; phi_x_idx = IfTrue; break; 353 case BoolTest::ge: cmp_zero_idx = 1; phi_x_idx = IfFalse; break; 354 default: return NULL; break; 355 } 356 357 // Find zero input of CmpD; the other input is being abs'd 358 Node *cmpd = bol->in(1); 359 if( cmpd->Opcode() != Op_CmpD ) return NULL; 360 Node *X = NULL; 361 bool flip = false; 362 if( phase->type(cmpd->in(cmp_zero_idx)) == TypeD::ZERO ) { 363 X = cmpd->in(3 - cmp_zero_idx); 364 } else if (phase->type(cmpd->in(3 - cmp_zero_idx)) == TypeD::ZERO) { 365 // The test is inverted, we should invert the result... 366 X = cmpd->in(cmp_zero_idx); 367 flip = true; 368 } else { 369 return NULL; 370 } 371 372 // If X is found on the appropriate phi input, find the subtract on the other 373 if( X != in(phi_x_idx) ) return NULL; 374 int phi_sub_idx = phi_x_idx == IfTrue ? IfFalse : IfTrue; 375 Node *sub = in(phi_sub_idx); 376 377 // Allow only SubD(0,X) and fail out for all others; NegD is not OK 378 if( sub->Opcode() != Op_SubD || 379 sub->in(2) != X || 380 phase->type(sub->in(1)) != TypeD::ZERO ) return NULL; 381 382 Node *abs = new (phase->C, 2) AbsDNode( X ); 383 if( flip ) 384 abs = new (phase->C, 3) SubDNode(sub->in(1), phase->transform(abs)); 385 386 return abs; 387 } 388 389 390 //============================================================================= 391 // If input is already higher or equal to cast type, then this is an identity. 392 Node *ConstraintCastNode::Identity( PhaseTransform *phase ) { 393 return phase->type(in(1))->higher_equal(_type) ? in(1) : this; 394 } 395 396 //------------------------------Value------------------------------------------ 397 // Take 'join' of input and cast-up type 398 const Type *ConstraintCastNode::Value( PhaseTransform *phase ) const { 399 if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP; 400 const Type* ft = phase->type(in(1))->filter(_type); 401 402 #ifdef ASSERT 403 // Previous versions of this function had some special case logic, 404 // which is no longer necessary. Make sure of the required effects. 405 switch (Opcode()) { 406 case Op_CastII: 407 { 408 const Type* t1 = phase->type(in(1)); 409 if( t1 == Type::TOP ) assert(ft == Type::TOP, "special case #1"); 410 const Type* rt = t1->join(_type); 411 if (rt->empty()) assert(ft == Type::TOP, "special case #2"); 412 break; 413 } 414 case Op_CastPP: 415 if (phase->type(in(1)) == TypePtr::NULL_PTR && 416 _type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull) 417 assert(ft == Type::TOP, "special case #3"); 418 break; 419 } 420 #endif //ASSERT 421 422 return ft; 423 } 424 425 //------------------------------Ideal------------------------------------------ 426 // Return a node which is more "ideal" than the current node. Strip out 427 // control copies 428 Node *ConstraintCastNode::Ideal(PhaseGVN *phase, bool can_reshape){ 429 return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL; 430 } 431 432 //------------------------------Ideal_DU_postCCP------------------------------- 433 // Throw away cast after constant propagation 434 Node *ConstraintCastNode::Ideal_DU_postCCP( PhaseCCP *ccp ) { 435 const Type *t = ccp->type(in(1)); 436 ccp->hash_delete(this); 437 set_type(t); // Turn into ID function 438 ccp->hash_insert(this); 439 return this; 440 } 441 442 443 //============================================================================= 444 445 //------------------------------Ideal_DU_postCCP------------------------------- 446 // If not converting int->oop, throw away cast after constant propagation 447 Node *CastPPNode::Ideal_DU_postCCP( PhaseCCP *ccp ) { 448 const Type *t = ccp->type(in(1)); 449 if (!t->isa_oop_ptr() || (in(1)->is_DecodeN() && Matcher::gen_narrow_oop_implicit_null_checks())) { 450 return NULL; // do not transform raw pointers or narrow oops 451 } 452 return ConstraintCastNode::Ideal_DU_postCCP(ccp); 453 } 454 455 456 457 //============================================================================= 458 //------------------------------Identity--------------------------------------- 459 // If input is already higher or equal to cast type, then this is an identity. 460 Node *CheckCastPPNode::Identity( PhaseTransform *phase ) { 461 // Toned down to rescue meeting at a Phi 3 different oops all implementing 462 // the same interface. CompileTheWorld starting at 502, kd12rc1.zip. 463 return (phase->type(in(1)) == phase->type(this)) ? in(1) : this; 464 } 465 466 // Determine whether "n" is a node which can cause an alias of one of its inputs. Node types 467 // which can create aliases are: CheckCastPP, Phi, and any store (if there is also a load from 468 // the location.) 469 // Note: this checks for aliases created in this compilation, not ones which may 470 // be potentially created at call sites. 471 static bool can_cause_alias(Node *n, PhaseTransform *phase) { 472 bool possible_alias = false; 473 474 if (n->is_Store()) { 475 possible_alias = !n->as_Store()->value_never_loaded(phase); 476 } else { 477 int opc = n->Opcode(); 478 possible_alias = n->is_Phi() || 479 opc == Op_CheckCastPP || 480 opc == Op_StorePConditional || 481 opc == Op_CompareAndSwapP || 482 opc == Op_CompareAndSwapN; 483 } 484 return possible_alias; 485 } 486 487 //------------------------------Value------------------------------------------ 488 // Take 'join' of input and cast-up type, unless working with an Interface 489 const Type *CheckCastPPNode::Value( PhaseTransform *phase ) const { 490 if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP; 491 492 const Type *inn = phase->type(in(1)); 493 if( inn == Type::TOP ) return Type::TOP; // No information yet 494 495 const TypePtr *in_type = inn->isa_ptr(); 496 const TypePtr *my_type = _type->isa_ptr(); 497 const Type *result = _type; 498 if( in_type != NULL && my_type != NULL ) { 499 TypePtr::PTR in_ptr = in_type->ptr(); 500 if( in_ptr == TypePtr::Null ) { 501 result = in_type; 502 } else if( in_ptr == TypePtr::Constant ) { 503 // Casting a constant oop to an interface? 504 // (i.e., a String to a Comparable?) 505 // Then return the interface. 506 const TypeOopPtr *jptr = my_type->isa_oopptr(); 507 assert( jptr, "" ); 508 result = (jptr->klass()->is_interface() || !in_type->higher_equal(_type)) 509 ? my_type->cast_to_ptr_type( TypePtr::NotNull ) 510 : in_type; 511 } else { 512 result = my_type->cast_to_ptr_type( my_type->join_ptr(in_ptr) ); 513 } 514 } 515 return result; 516 517 // JOIN NOT DONE HERE BECAUSE OF INTERFACE ISSUES. 518 // FIX THIS (DO THE JOIN) WHEN UNION TYPES APPEAR! 519 520 // 521 // Remove this code after overnight run indicates no performance 522 // loss from not performing JOIN at CheckCastPPNode 523 // 524 // const TypeInstPtr *in_oop = in->isa_instptr(); 525 // const TypeInstPtr *my_oop = _type->isa_instptr(); 526 // // If either input is an 'interface', return destination type 527 // assert (in_oop == NULL || in_oop->klass() != NULL, ""); 528 // assert (my_oop == NULL || my_oop->klass() != NULL, ""); 529 // if( (in_oop && in_oop->klass()->klass_part()->is_interface()) 530 // ||(my_oop && my_oop->klass()->klass_part()->is_interface()) ) { 531 // TypePtr::PTR in_ptr = in->isa_ptr() ? in->is_ptr()->_ptr : TypePtr::BotPTR; 532 // // Preserve cast away nullness for interfaces 533 // if( in_ptr == TypePtr::NotNull && my_oop && my_oop->_ptr == TypePtr::BotPTR ) { 534 // return my_oop->cast_to_ptr_type(TypePtr::NotNull); 535 // } 536 // return _type; 537 // } 538 // 539 // // Neither the input nor the destination type is an interface, 540 // 541 // // history: JOIN used to cause weird corner case bugs 542 // // return (in == TypeOopPtr::NULL_PTR) ? in : _type; 543 // // JOIN picks up NotNull in common instance-of/check-cast idioms, both oops. 544 // // JOIN does not preserve NotNull in other cases, e.g. RawPtr vs InstPtr 545 // const Type *join = in->join(_type); 546 // // Check if join preserved NotNull'ness for pointers 547 // if( join->isa_ptr() && _type->isa_ptr() ) { 548 // TypePtr::PTR join_ptr = join->is_ptr()->_ptr; 549 // TypePtr::PTR type_ptr = _type->is_ptr()->_ptr; 550 // // If there isn't any NotNull'ness to preserve 551 // // OR if join preserved NotNull'ness then return it 552 // if( type_ptr == TypePtr::BotPTR || type_ptr == TypePtr::Null || 553 // join_ptr == TypePtr::NotNull || join_ptr == TypePtr::Constant ) { 554 // return join; 555 // } 556 // // ELSE return same old type as before 557 // return _type; 558 // } 559 // // Not joining two pointers 560 // return join; 561 } 562 563 //------------------------------Ideal------------------------------------------ 564 // Return a node which is more "ideal" than the current node. Strip out 565 // control copies 566 Node *CheckCastPPNode::Ideal(PhaseGVN *phase, bool can_reshape){ 567 return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL; 568 } 569 570 571 Node* DecodeNNode::Identity(PhaseTransform* phase) { 572 const Type *t = phase->type( in(1) ); 573 if( t == Type::TOP ) return in(1); 574 575 if (in(1)->is_EncodeP()) { 576 // (DecodeN (EncodeP p)) -> p 577 return in(1)->in(1); 578 } 579 return this; 580 } 581 582 const Type *DecodeNNode::Value( PhaseTransform *phase ) const { 583 const Type *t = phase->type( in(1) ); 584 if (t == Type::TOP) return Type::TOP; 585 if (t == TypeNarrowOop::NULL_PTR) return TypePtr::NULL_PTR; 586 587 assert(t->isa_narrowoop(), "only narrowoop here"); 588 return t->make_ptr(); 589 } 590 591 Node* EncodePNode::Identity(PhaseTransform* phase) { 592 const Type *t = phase->type( in(1) ); 593 if( t == Type::TOP ) return in(1); 594 595 if (in(1)->is_DecodeN()) { 596 // (EncodeP (DecodeN p)) -> p 597 return in(1)->in(1); 598 } 599 return this; 600 } 601 602 const Type *EncodePNode::Value( PhaseTransform *phase ) const { 603 const Type *t = phase->type( in(1) ); 604 if (t == Type::TOP) return Type::TOP; 605 if (t == TypePtr::NULL_PTR) return TypeNarrowOop::NULL_PTR; 606 607 assert(t->isa_oopptr(), "only oopptr here"); 608 return t->make_narrowoop(); 609 } 610 611 612 Node *EncodePNode::Ideal_DU_postCCP( PhaseCCP *ccp ) { 613 return MemNode::Ideal_common_DU_postCCP(ccp, this, in(1)); 614 } 615 616 //============================================================================= 617 //------------------------------Identity--------------------------------------- 618 Node *Conv2BNode::Identity( PhaseTransform *phase ) { 619 const Type *t = phase->type( in(1) ); 620 if( t == Type::TOP ) return in(1); 621 if( t == TypeInt::ZERO ) return in(1); 622 if( t == TypeInt::ONE ) return in(1); 623 if( t == TypeInt::BOOL ) return in(1); 624 return this; 625 } 626 627 //------------------------------Value------------------------------------------ 628 const Type *Conv2BNode::Value( PhaseTransform *phase ) const { 629 const Type *t = phase->type( in(1) ); 630 if( t == Type::TOP ) return Type::TOP; 631 if( t == TypeInt::ZERO ) return TypeInt::ZERO; 632 if( t == TypePtr::NULL_PTR ) return TypeInt::ZERO; 633 const TypePtr *tp = t->isa_ptr(); 634 if( tp != NULL ) { 635 if( tp->ptr() == TypePtr::AnyNull ) return Type::TOP; 636 if( tp->ptr() == TypePtr::Constant) return TypeInt::ONE; 637 if (tp->ptr() == TypePtr::NotNull) return TypeInt::ONE; 638 return TypeInt::BOOL; 639 } 640 if (t->base() != Type::Int) return TypeInt::BOOL; 641 const TypeInt *ti = t->is_int(); 642 if( ti->_hi < 0 || ti->_lo > 0 ) return TypeInt::ONE; 643 return TypeInt::BOOL; 644 } 645 646 647 // The conversions operations are all Alpha sorted. Please keep it that way! 648 //============================================================================= 649 //------------------------------Value------------------------------------------ 650 const Type *ConvD2FNode::Value( PhaseTransform *phase ) const { 651 const Type *t = phase->type( in(1) ); 652 if( t == Type::TOP ) return Type::TOP; 653 if( t == Type::DOUBLE ) return Type::FLOAT; 654 const TypeD *td = t->is_double_constant(); 655 return TypeF::make( (float)td->getd() ); 656 } 657 658 //------------------------------Identity--------------------------------------- 659 // Float's can be converted to doubles with no loss of bits. Hence 660 // converting a float to a double and back to a float is a NOP. 661 Node *ConvD2FNode::Identity(PhaseTransform *phase) { 662 return (in(1)->Opcode() == Op_ConvF2D) ? in(1)->in(1) : this; 663 } 664 665 //============================================================================= 666 //------------------------------Value------------------------------------------ 667 const Type *ConvD2INode::Value( PhaseTransform *phase ) const { 668 const Type *t = phase->type( in(1) ); 669 if( t == Type::TOP ) return Type::TOP; 670 if( t == Type::DOUBLE ) return TypeInt::INT; 671 const TypeD *td = t->is_double_constant(); 672 return TypeInt::make( SharedRuntime::d2i( td->getd() ) ); 673 } 674 675 //------------------------------Ideal------------------------------------------ 676 // If converting to an int type, skip any rounding nodes 677 Node *ConvD2INode::Ideal(PhaseGVN *phase, bool can_reshape) { 678 if( in(1)->Opcode() == Op_RoundDouble ) 679 set_req(1,in(1)->in(1)); 680 return NULL; 681 } 682 683 //------------------------------Identity--------------------------------------- 684 // Int's can be converted to doubles with no loss of bits. Hence 685 // converting an integer to a double and back to an integer is a NOP. 686 Node *ConvD2INode::Identity(PhaseTransform *phase) { 687 return (in(1)->Opcode() == Op_ConvI2D) ? in(1)->in(1) : this; 688 } 689 690 //============================================================================= 691 //------------------------------Value------------------------------------------ 692 const Type *ConvD2LNode::Value( PhaseTransform *phase ) const { 693 const Type *t = phase->type( in(1) ); 694 if( t == Type::TOP ) return Type::TOP; 695 if( t == Type::DOUBLE ) return TypeLong::LONG; 696 const TypeD *td = t->is_double_constant(); 697 return TypeLong::make( SharedRuntime::d2l( td->getd() ) ); 698 } 699 700 //------------------------------Identity--------------------------------------- 701 Node *ConvD2LNode::Identity(PhaseTransform *phase) { 702 // Remove ConvD2L->ConvL2D->ConvD2L sequences. 703 if( in(1) ->Opcode() == Op_ConvL2D && 704 in(1)->in(1)->Opcode() == Op_ConvD2L ) 705 return in(1)->in(1); 706 return this; 707 } 708 709 //------------------------------Ideal------------------------------------------ 710 // If converting to an int type, skip any rounding nodes 711 Node *ConvD2LNode::Ideal(PhaseGVN *phase, bool can_reshape) { 712 if( in(1)->Opcode() == Op_RoundDouble ) 713 set_req(1,in(1)->in(1)); 714 return NULL; 715 } 716 717 //============================================================================= 718 //------------------------------Value------------------------------------------ 719 const Type *ConvF2DNode::Value( PhaseTransform *phase ) const { 720 const Type *t = phase->type( in(1) ); 721 if( t == Type::TOP ) return Type::TOP; 722 if( t == Type::FLOAT ) return Type::DOUBLE; 723 const TypeF *tf = t->is_float_constant(); 724 return TypeD::make( (double)tf->getf() ); 725 } 726 727 //============================================================================= 728 //------------------------------Value------------------------------------------ 729 const Type *ConvF2INode::Value( PhaseTransform *phase ) const { 730 const Type *t = phase->type( in(1) ); 731 if( t == Type::TOP ) return Type::TOP; 732 if( t == Type::FLOAT ) return TypeInt::INT; 733 const TypeF *tf = t->is_float_constant(); 734 return TypeInt::make( SharedRuntime::f2i( tf->getf() ) ); 735 } 736 737 //------------------------------Identity--------------------------------------- 738 Node *ConvF2INode::Identity(PhaseTransform *phase) { 739 // Remove ConvF2I->ConvI2F->ConvF2I sequences. 740 if( in(1) ->Opcode() == Op_ConvI2F && 741 in(1)->in(1)->Opcode() == Op_ConvF2I ) 742 return in(1)->in(1); 743 return this; 744 } 745 746 //------------------------------Ideal------------------------------------------ 747 // If converting to an int type, skip any rounding nodes 748 Node *ConvF2INode::Ideal(PhaseGVN *phase, bool can_reshape) { 749 if( in(1)->Opcode() == Op_RoundFloat ) 750 set_req(1,in(1)->in(1)); 751 return NULL; 752 } 753 754 //============================================================================= 755 //------------------------------Value------------------------------------------ 756 const Type *ConvF2LNode::Value( PhaseTransform *phase ) const { 757 const Type *t = phase->type( in(1) ); 758 if( t == Type::TOP ) return Type::TOP; 759 if( t == Type::FLOAT ) return TypeLong::LONG; 760 const TypeF *tf = t->is_float_constant(); 761 return TypeLong::make( SharedRuntime::f2l( tf->getf() ) ); 762 } 763 764 //------------------------------Identity--------------------------------------- 765 Node *ConvF2LNode::Identity(PhaseTransform *phase) { 766 // Remove ConvF2L->ConvL2F->ConvF2L sequences. 767 if( in(1) ->Opcode() == Op_ConvL2F && 768 in(1)->in(1)->Opcode() == Op_ConvF2L ) 769 return in(1)->in(1); 770 return this; 771 } 772 773 //------------------------------Ideal------------------------------------------ 774 // If converting to an int type, skip any rounding nodes 775 Node *ConvF2LNode::Ideal(PhaseGVN *phase, bool can_reshape) { 776 if( in(1)->Opcode() == Op_RoundFloat ) 777 set_req(1,in(1)->in(1)); 778 return NULL; 779 } 780 781 //============================================================================= 782 //------------------------------Value------------------------------------------ 783 const Type *ConvI2DNode::Value( PhaseTransform *phase ) const { 784 const Type *t = phase->type( in(1) ); 785 if( t == Type::TOP ) return Type::TOP; 786 const TypeInt *ti = t->is_int(); 787 if( ti->is_con() ) return TypeD::make( (double)ti->get_con() ); 788 return bottom_type(); 789 } 790 791 //============================================================================= 792 //------------------------------Value------------------------------------------ 793 const Type *ConvI2FNode::Value( PhaseTransform *phase ) const { 794 const Type *t = phase->type( in(1) ); 795 if( t == Type::TOP ) return Type::TOP; 796 const TypeInt *ti = t->is_int(); 797 if( ti->is_con() ) return TypeF::make( (float)ti->get_con() ); 798 return bottom_type(); 799 } 800 801 //------------------------------Identity--------------------------------------- 802 Node *ConvI2FNode::Identity(PhaseTransform *phase) { 803 // Remove ConvI2F->ConvF2I->ConvI2F sequences. 804 if( in(1) ->Opcode() == Op_ConvF2I && 805 in(1)->in(1)->Opcode() == Op_ConvI2F ) 806 return in(1)->in(1); 807 return this; 808 } 809 810 //============================================================================= 811 //------------------------------Value------------------------------------------ 812 const Type *ConvI2LNode::Value( PhaseTransform *phase ) const { 813 const Type *t = phase->type( in(1) ); 814 if( t == Type::TOP ) return Type::TOP; 815 const TypeInt *ti = t->is_int(); 816 const Type* tl = TypeLong::make(ti->_lo, ti->_hi, ti->_widen); 817 // Join my declared type against my incoming type. 818 tl = tl->filter(_type); 819 return tl; 820 } 821 822 #ifdef _LP64 823 static inline bool long_ranges_overlap(jlong lo1, jlong hi1, 824 jlong lo2, jlong hi2) { 825 // Two ranges overlap iff one range's low point falls in the other range. 826 return (lo2 <= lo1 && lo1 <= hi2) || (lo1 <= lo2 && lo2 <= hi1); 827 } 828 #endif 829 830 //------------------------------Ideal------------------------------------------ 831 Node *ConvI2LNode::Ideal(PhaseGVN *phase, bool can_reshape) { 832 const TypeLong* this_type = this->type()->is_long(); 833 Node* this_changed = NULL; 834 835 // If _major_progress, then more loop optimizations follow. Do NOT 836 // remove this node's type assertion until no more loop ops can happen. 837 // The progress bit is set in the major loop optimizations THEN comes the 838 // call to IterGVN and any chance of hitting this code. Cf. Opaque1Node. 839 if (can_reshape && !phase->C->major_progress()) { 840 const TypeInt* in_type = phase->type(in(1))->isa_int(); 841 if (in_type != NULL && this_type != NULL && 842 (in_type->_lo != this_type->_lo || 843 in_type->_hi != this_type->_hi)) { 844 // Although this WORSENS the type, it increases GVN opportunities, 845 // because I2L nodes with the same input will common up, regardless 846 // of slightly differing type assertions. Such slight differences 847 // arise routinely as a result of loop unrolling, so this is a 848 // post-unrolling graph cleanup. Choose a type which depends only 849 // on my input. (Exception: Keep a range assertion of >=0 or <0.) 850 jlong lo1 = this_type->_lo; 851 jlong hi1 = this_type->_hi; 852 int w1 = this_type->_widen; 853 if (lo1 != (jint)lo1 || 854 hi1 != (jint)hi1 || 855 lo1 > hi1) { 856 // Overflow leads to wraparound, wraparound leads to range saturation. 857 lo1 = min_jint; hi1 = max_jint; 858 } else if (lo1 >= 0) { 859 // Keep a range assertion of >=0. 860 lo1 = 0; hi1 = max_jint; 861 } else if (hi1 < 0) { 862 // Keep a range assertion of <0. 863 lo1 = min_jint; hi1 = -1; 864 } else { 865 lo1 = min_jint; hi1 = max_jint; 866 } 867 const TypeLong* wtype = TypeLong::make(MAX2((jlong)in_type->_lo, lo1), 868 MIN2((jlong)in_type->_hi, hi1), 869 MAX2((int)in_type->_widen, w1)); 870 if (wtype != type()) { 871 set_type(wtype); 872 // Note: this_type still has old type value, for the logic below. 873 this_changed = this; 874 } 875 } 876 } 877 878 #ifdef _LP64 879 // Convert ConvI2L(AddI(x, y)) to AddL(ConvI2L(x), ConvI2L(y)) , 880 // but only if x and y have subranges that cannot cause 32-bit overflow, 881 // under the assumption that x+y is in my own subrange this->type(). 882 883 // This assumption is based on a constraint (i.e., type assertion) 884 // established in Parse::array_addressing or perhaps elsewhere. 885 // This constraint has been adjoined to the "natural" type of 886 // the incoming argument in(0). We know (because of runtime 887 // checks) - that the result value I2L(x+y) is in the joined range. 888 // Hence we can restrict the incoming terms (x, y) to values such 889 // that their sum also lands in that range. 890 891 // This optimization is useful only on 64-bit systems, where we hope 892 // the addition will end up subsumed in an addressing mode. 893 // It is necessary to do this when optimizing an unrolled array 894 // copy loop such as x[i++] = y[i++]. 895 896 // On 32-bit systems, it's better to perform as much 32-bit math as 897 // possible before the I2L conversion, because 32-bit math is cheaper. 898 // There's no common reason to "leak" a constant offset through the I2L. 899 // Addressing arithmetic will not absorb it as part of a 64-bit AddL. 900 901 Node* z = in(1); 902 int op = z->Opcode(); 903 if (op == Op_AddI || op == Op_SubI) { 904 Node* x = z->in(1); 905 Node* y = z->in(2); 906 assert (x != z && y != z, "dead loop in ConvI2LNode::Ideal"); 907 if (phase->type(x) == Type::TOP) return this_changed; 908 if (phase->type(y) == Type::TOP) return this_changed; 909 const TypeInt* tx = phase->type(x)->is_int(); 910 const TypeInt* ty = phase->type(y)->is_int(); 911 const TypeLong* tz = this_type; 912 jlong xlo = tx->_lo; 913 jlong xhi = tx->_hi; 914 jlong ylo = ty->_lo; 915 jlong yhi = ty->_hi; 916 jlong zlo = tz->_lo; 917 jlong zhi = tz->_hi; 918 jlong vbit = CONST64(1) << BitsPerInt; 919 int widen = MAX2(tx->_widen, ty->_widen); 920 if (op == Op_SubI) { 921 jlong ylo0 = ylo; 922 ylo = -yhi; 923 yhi = -ylo0; 924 } 925 // See if x+y can cause positive overflow into z+2**32 926 if (long_ranges_overlap(xlo+ylo, xhi+yhi, zlo+vbit, zhi+vbit)) { 927 return this_changed; 928 } 929 // See if x+y can cause negative overflow into z-2**32 930 if (long_ranges_overlap(xlo+ylo, xhi+yhi, zlo-vbit, zhi-vbit)) { 931 return this_changed; 932 } 933 // Now it's always safe to assume x+y does not overflow. 934 // This is true even if some pairs x,y might cause overflow, as long 935 // as that overflow value cannot fall into [zlo,zhi]. 936 937 // Confident that the arithmetic is "as if infinite precision", 938 // we can now use z's range to put constraints on those of x and y. 939 // The "natural" range of x [xlo,xhi] can perhaps be narrowed to a 940 // more "restricted" range by intersecting [xlo,xhi] with the 941 // range obtained by subtracting y's range from the asserted range 942 // of the I2L conversion. Here's the interval arithmetic algebra: 943 // x == z-y == [zlo,zhi]-[ylo,yhi] == [zlo,zhi]+[-yhi,-ylo] 944 // => x in [zlo-yhi, zhi-ylo] 945 // => x in [zlo-yhi, zhi-ylo] INTERSECT [xlo,xhi] 946 // => x in [xlo MAX zlo-yhi, xhi MIN zhi-ylo] 947 jlong rxlo = MAX2(xlo, zlo - yhi); 948 jlong rxhi = MIN2(xhi, zhi - ylo); 949 // And similarly, x changing place with y: 950 jlong rylo = MAX2(ylo, zlo - xhi); 951 jlong ryhi = MIN2(yhi, zhi - xlo); 952 if (rxlo > rxhi || rylo > ryhi) { 953 return this_changed; // x or y is dying; don't mess w/ it 954 } 955 if (op == Op_SubI) { 956 jlong rylo0 = rylo; 957 rylo = -ryhi; 958 ryhi = -rylo0; 959 } 960 961 Node* cx = phase->transform( new (phase->C, 2) ConvI2LNode(x, TypeLong::make(rxlo, rxhi, widen)) ); 962 Node* cy = phase->transform( new (phase->C, 2) ConvI2LNode(y, TypeLong::make(rylo, ryhi, widen)) ); 963 switch (op) { 964 case Op_AddI: return new (phase->C, 3) AddLNode(cx, cy); 965 case Op_SubI: return new (phase->C, 3) SubLNode(cx, cy); 966 default: ShouldNotReachHere(); 967 } 968 } 969 #endif //_LP64 970 971 return this_changed; 972 } 973 974 //============================================================================= 975 //------------------------------Value------------------------------------------ 976 const Type *ConvL2DNode::Value( PhaseTransform *phase ) const { 977 const Type *t = phase->type( in(1) ); 978 if( t == Type::TOP ) return Type::TOP; 979 const TypeLong *tl = t->is_long(); 980 if( tl->is_con() ) return TypeD::make( (double)tl->get_con() ); 981 return bottom_type(); 982 } 983 984 //============================================================================= 985 //------------------------------Value------------------------------------------ 986 const Type *ConvL2FNode::Value( PhaseTransform *phase ) const { 987 const Type *t = phase->type( in(1) ); 988 if( t == Type::TOP ) return Type::TOP; 989 const TypeLong *tl = t->is_long(); 990 if( tl->is_con() ) return TypeF::make( (float)tl->get_con() ); 991 return bottom_type(); 992 } 993 994 //============================================================================= 995 //----------------------------Identity----------------------------------------- 996 Node *ConvL2INode::Identity( PhaseTransform *phase ) { 997 // Convert L2I(I2L(x)) => x 998 if (in(1)->Opcode() == Op_ConvI2L) return in(1)->in(1); 999 return this; 1000 } 1001 1002 //------------------------------Value------------------------------------------ 1003 const Type *ConvL2INode::Value( PhaseTransform *phase ) const { 1004 const Type *t = phase->type( in(1) ); 1005 if( t == Type::TOP ) return Type::TOP; 1006 const TypeLong *tl = t->is_long(); 1007 if (tl->is_con()) 1008 // Easy case. 1009 return TypeInt::make((jint)tl->get_con()); 1010 return bottom_type(); 1011 } 1012 1013 //------------------------------Ideal------------------------------------------ 1014 // Return a node which is more "ideal" than the current node. 1015 // Blow off prior masking to int 1016 Node *ConvL2INode::Ideal(PhaseGVN *phase, bool can_reshape) { 1017 Node *andl = in(1); 1018 uint andl_op = andl->Opcode(); 1019 if( andl_op == Op_AndL ) { 1020 // Blow off prior masking to int 1021 if( phase->type(andl->in(2)) == TypeLong::make( 0xFFFFFFFF ) ) { 1022 set_req(1,andl->in(1)); 1023 return this; 1024 } 1025 } 1026 1027 // Swap with a prior add: convL2I(addL(x,y)) ==> addI(convL2I(x),convL2I(y)) 1028 // This replaces an 'AddL' with an 'AddI'. 1029 if( andl_op == Op_AddL ) { 1030 // Don't do this for nodes which have more than one user since 1031 // we'll end up computing the long add anyway. 1032 if (andl->outcnt() > 1) return NULL; 1033 1034 Node* x = andl->in(1); 1035 Node* y = andl->in(2); 1036 assert( x != andl && y != andl, "dead loop in ConvL2INode::Ideal" ); 1037 if (phase->type(x) == Type::TOP) return NULL; 1038 if (phase->type(y) == Type::TOP) return NULL; 1039 Node *add1 = phase->transform(new (phase->C, 2) ConvL2INode(x)); 1040 Node *add2 = phase->transform(new (phase->C, 2) ConvL2INode(y)); 1041 return new (phase->C, 3) AddINode(add1,add2); 1042 } 1043 1044 // Disable optimization: LoadL->ConvL2I ==> LoadI. 1045 // It causes problems (sizes of Load and Store nodes do not match) 1046 // in objects initialization code and Escape Analysis. 1047 return NULL; 1048 } 1049 1050 //============================================================================= 1051 //------------------------------Value------------------------------------------ 1052 const Type *CastX2PNode::Value( PhaseTransform *phase ) const { 1053 const Type* t = phase->type(in(1)); 1054 if (t == Type::TOP) return Type::TOP; 1055 if (t->base() == Type_X && t->singleton()) { 1056 uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con(); 1057 if (bits == 0) return TypePtr::NULL_PTR; 1058 return TypeRawPtr::make((address) bits); 1059 } 1060 return CastX2PNode::bottom_type(); 1061 } 1062 1063 //------------------------------Idealize--------------------------------------- 1064 static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) { 1065 if (t == Type::TOP) return false; 1066 const TypeX* tl = t->is_intptr_t(); 1067 jint lo = min_jint; 1068 jint hi = max_jint; 1069 if (but_not_min_int) ++lo; // caller wants to negate the value w/o overflow 1070 return (tl->_lo >= lo) && (tl->_hi <= hi); 1071 } 1072 1073 static inline Node* addP_of_X2P(PhaseGVN *phase, 1074 Node* base, 1075 Node* dispX, 1076 bool negate = false) { 1077 if (negate) { 1078 dispX = new (phase->C, 3) SubXNode(phase->MakeConX(0), phase->transform(dispX)); 1079 } 1080 return new (phase->C, 4) AddPNode(phase->C->top(), 1081 phase->transform(new (phase->C, 2) CastX2PNode(base)), 1082 phase->transform(dispX)); 1083 } 1084 1085 Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1086 // convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int 1087 int op = in(1)->Opcode(); 1088 Node* x; 1089 Node* y; 1090 switch (op) { 1091 case Op_SubX: 1092 x = in(1)->in(1); 1093 // Avoid ideal transformations ping-pong between this and AddP for raw pointers. 1094 if (phase->find_intptr_t_con(x, -1) == 0) 1095 break; 1096 y = in(1)->in(2); 1097 if (fits_in_int(phase->type(y), true)) { 1098 return addP_of_X2P(phase, x, y, true); 1099 } 1100 break; 1101 case Op_AddX: 1102 x = in(1)->in(1); 1103 y = in(1)->in(2); 1104 if (fits_in_int(phase->type(y))) { 1105 return addP_of_X2P(phase, x, y); 1106 } 1107 if (fits_in_int(phase->type(x))) { 1108 return addP_of_X2P(phase, y, x); 1109 } 1110 break; 1111 } 1112 return NULL; 1113 } 1114 1115 //------------------------------Identity--------------------------------------- 1116 Node *CastX2PNode::Identity( PhaseTransform *phase ) { 1117 if (in(1)->Opcode() == Op_CastP2X) return in(1)->in(1); 1118 return this; 1119 } 1120 1121 //============================================================================= 1122 //------------------------------Value------------------------------------------ 1123 const Type *CastP2XNode::Value( PhaseTransform *phase ) const { 1124 const Type* t = phase->type(in(1)); 1125 if (t == Type::TOP) return Type::TOP; 1126 if (t->base() == Type::RawPtr && t->singleton()) { 1127 uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con(); 1128 return TypeX::make(bits); 1129 } 1130 return CastP2XNode::bottom_type(); 1131 } 1132 1133 Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1134 return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL; 1135 } 1136 1137 //------------------------------Identity--------------------------------------- 1138 Node *CastP2XNode::Identity( PhaseTransform *phase ) { 1139 if (in(1)->Opcode() == Op_CastX2P) return in(1)->in(1); 1140 return this; 1141 } 1142 1143 1144 //============================================================================= 1145 //------------------------------Identity--------------------------------------- 1146 // Remove redundant roundings 1147 Node *RoundFloatNode::Identity( PhaseTransform *phase ) { 1148 assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel"); 1149 // Do not round constants 1150 if (phase->type(in(1))->base() == Type::FloatCon) return in(1); 1151 int op = in(1)->Opcode(); 1152 // Redundant rounding 1153 if( op == Op_RoundFloat ) return in(1); 1154 // Already rounded 1155 if( op == Op_Parm ) return in(1); 1156 if( op == Op_LoadF ) return in(1); 1157 return this; 1158 } 1159 1160 //------------------------------Value------------------------------------------ 1161 const Type *RoundFloatNode::Value( PhaseTransform *phase ) const { 1162 return phase->type( in(1) ); 1163 } 1164 1165 //============================================================================= 1166 //------------------------------Identity--------------------------------------- 1167 // Remove redundant roundings. Incoming arguments are already rounded. 1168 Node *RoundDoubleNode::Identity( PhaseTransform *phase ) { 1169 assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel"); 1170 // Do not round constants 1171 if (phase->type(in(1))->base() == Type::DoubleCon) return in(1); 1172 int op = in(1)->Opcode(); 1173 // Redundant rounding 1174 if( op == Op_RoundDouble ) return in(1); 1175 // Already rounded 1176 if( op == Op_Parm ) return in(1); 1177 if( op == Op_LoadD ) return in(1); 1178 if( op == Op_ConvF2D ) return in(1); 1179 if( op == Op_ConvI2D ) return in(1); 1180 return this; 1181 } 1182 1183 //------------------------------Value------------------------------------------ 1184 const Type *RoundDoubleNode::Value( PhaseTransform *phase ) const { 1185 return phase->type( in(1) ); 1186 } 1187 1188 1189 //============================================================================= 1190 // Do not allow value-numbering 1191 uint Opaque1Node::hash() const { return NO_HASH; } 1192 uint Opaque1Node::cmp( const Node &n ) const { 1193 return (&n == this); // Always fail except on self 1194 } 1195 1196 //------------------------------Identity--------------------------------------- 1197 // If _major_progress, then more loop optimizations follow. Do NOT remove 1198 // the opaque Node until no more loop ops can happen. Note the timing of 1199 // _major_progress; it's set in the major loop optimizations THEN comes the 1200 // call to IterGVN and any chance of hitting this code. Hence there's no 1201 // phase-ordering problem with stripping Opaque1 in IGVN followed by some 1202 // more loop optimizations that require it. 1203 Node *Opaque1Node::Identity( PhaseTransform *phase ) { 1204 return phase->C->major_progress() ? this : in(1); 1205 } 1206 1207 //============================================================================= 1208 // A node to prevent unwanted optimizations. Allows constant folding. Stops 1209 // value-numbering, most Ideal calls or Identity functions. This Node is 1210 // specifically designed to prevent the pre-increment value of a loop trip 1211 // counter from being live out of the bottom of the loop (hence causing the 1212 // pre- and post-increment values both being live and thus requiring an extra 1213 // temp register and an extra move). If we "accidentally" optimize through 1214 // this kind of a Node, we'll get slightly pessimal, but correct, code. Thus 1215 // it's OK to be slightly sloppy on optimizations here. 1216 1217 // Do not allow value-numbering 1218 uint Opaque2Node::hash() const { return NO_HASH; } 1219 uint Opaque2Node::cmp( const Node &n ) const { 1220 return (&n == this); // Always fail except on self 1221 } 1222 1223 1224 //------------------------------Value------------------------------------------ 1225 const Type *MoveL2DNode::Value( PhaseTransform *phase ) const { 1226 const Type *t = phase->type( in(1) ); 1227 if( t == Type::TOP ) return Type::TOP; 1228 const TypeLong *tl = t->is_long(); 1229 if( !tl->is_con() ) return bottom_type(); 1230 JavaValue v; 1231 v.set_jlong(tl->get_con()); 1232 return TypeD::make( v.get_jdouble() ); 1233 } 1234 1235 //------------------------------Value------------------------------------------ 1236 const Type *MoveI2FNode::Value( PhaseTransform *phase ) const { 1237 const Type *t = phase->type( in(1) ); 1238 if( t == Type::TOP ) return Type::TOP; 1239 const TypeInt *ti = t->is_int(); 1240 if( !ti->is_con() ) return bottom_type(); 1241 JavaValue v; 1242 v.set_jint(ti->get_con()); 1243 return TypeF::make( v.get_jfloat() ); 1244 } 1245 1246 //------------------------------Value------------------------------------------ 1247 const Type *MoveF2INode::Value( PhaseTransform *phase ) const { 1248 const Type *t = phase->type( in(1) ); 1249 if( t == Type::TOP ) return Type::TOP; 1250 if( t == Type::FLOAT ) return TypeInt::INT; 1251 const TypeF *tf = t->is_float_constant(); 1252 JavaValue v; 1253 v.set_jfloat(tf->getf()); 1254 return TypeInt::make( v.get_jint() ); 1255 } 1256 1257 //------------------------------Value------------------------------------------ 1258 const Type *MoveD2LNode::Value( PhaseTransform *phase ) const { 1259 const Type *t = phase->type( in(1) ); 1260 if( t == Type::TOP ) return Type::TOP; 1261 if( t == Type::DOUBLE ) return TypeLong::LONG; 1262 const TypeD *td = t->is_double_constant(); 1263 JavaValue v; 1264 v.set_jdouble(td->getd()); 1265 return TypeLong::make( v.get_jlong() ); 1266 } 1267 1268 //------------------------------Value------------------------------------------ 1269 const Type* CountLeadingZerosINode::Value(PhaseTransform* phase) const { 1270 const Type* t = phase->type(in(1)); 1271 if (t == Type::TOP) return Type::TOP; 1272 const TypeInt* ti = t->isa_int(); 1273 if (ti && ti->is_con()) { 1274 jint i = ti->get_con(); 1275 // HD, Figure 5-6 1276 if (i == 0) 1277 return TypeInt::make(BitsPerInt); 1278 int n = 1; 1279 unsigned int x = i; 1280 if (x >> 16 == 0) { n += 16; x <<= 16; } 1281 if (x >> 24 == 0) { n += 8; x <<= 8; } 1282 if (x >> 28 == 0) { n += 4; x <<= 4; } 1283 if (x >> 30 == 0) { n += 2; x <<= 2; } 1284 n -= x >> 31; 1285 return TypeInt::make(n); 1286 } 1287 return TypeInt::INT; 1288 } 1289 1290 //------------------------------Value------------------------------------------ 1291 const Type* CountLeadingZerosLNode::Value(PhaseTransform* phase) const { 1292 const Type* t = phase->type(in(1)); 1293 if (t == Type::TOP) return Type::TOP; 1294 const TypeLong* tl = t->isa_long(); 1295 if (tl && tl->is_con()) { 1296 jlong l = tl->get_con(); 1297 // HD, Figure 5-6 1298 if (l == 0) 1299 return TypeInt::make(BitsPerLong); 1300 int n = 1; 1301 unsigned int x = (((julong) l) >> 32); 1302 if (x == 0) { n += 32; x = (int) l; } 1303 if (x >> 16 == 0) { n += 16; x <<= 16; } 1304 if (x >> 24 == 0) { n += 8; x <<= 8; } 1305 if (x >> 28 == 0) { n += 4; x <<= 4; } 1306 if (x >> 30 == 0) { n += 2; x <<= 2; } 1307 n -= x >> 31; 1308 return TypeInt::make(n); 1309 } 1310 return TypeInt::INT; 1311 } 1312 1313 //------------------------------Value------------------------------------------ 1314 const Type* CountTrailingZerosINode::Value(PhaseTransform* phase) const { 1315 const Type* t = phase->type(in(1)); 1316 if (t == Type::TOP) return Type::TOP; 1317 const TypeInt* ti = t->isa_int(); 1318 if (ti && ti->is_con()) { 1319 jint i = ti->get_con(); 1320 // HD, Figure 5-14 1321 int y; 1322 if (i == 0) 1323 return TypeInt::make(BitsPerInt); 1324 int n = 31; 1325 y = i << 16; if (y != 0) { n = n - 16; i = y; } 1326 y = i << 8; if (y != 0) { n = n - 8; i = y; } 1327 y = i << 4; if (y != 0) { n = n - 4; i = y; } 1328 y = i << 2; if (y != 0) { n = n - 2; i = y; } 1329 y = i << 1; if (y != 0) { n = n - 1; } 1330 return TypeInt::make(n); 1331 } 1332 return TypeInt::INT; 1333 } 1334 1335 //------------------------------Value------------------------------------------ 1336 const Type* CountTrailingZerosLNode::Value(PhaseTransform* phase) const { 1337 const Type* t = phase->type(in(1)); 1338 if (t == Type::TOP) return Type::TOP; 1339 const TypeLong* tl = t->isa_long(); 1340 if (tl && tl->is_con()) { 1341 jlong l = tl->get_con(); 1342 // HD, Figure 5-14 1343 int x, y; 1344 if (l == 0) 1345 return TypeInt::make(BitsPerLong); 1346 int n = 63; 1347 y = (int) l; if (y != 0) { n = n - 32; x = y; } else x = (((julong) l) >> 32); 1348 y = x << 16; if (y != 0) { n = n - 16; x = y; } 1349 y = x << 8; if (y != 0) { n = n - 8; x = y; } 1350 y = x << 4; if (y != 0) { n = n - 4; x = y; } 1351 y = x << 2; if (y != 0) { n = n - 2; x = y; } 1352 y = x << 1; if (y != 0) { n = n - 1; } 1353 return TypeInt::make(n); 1354 } 1355 return TypeInt::INT; 1356 }