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