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