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