subnode.cpp revision 844:bd02caa94611
1/*
2 * Copyright 1997-2009 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// Portions of code courtesy of Clifford Click
26
27// Optimization - Graph Style
28
29#include "incls/_precompiled.incl"
30#include "incls/_subnode.cpp.incl"
31#include "math.h"
32
33//=============================================================================
34//------------------------------Identity---------------------------------------
35// If right input is a constant 0, return the left input.
36Node *SubNode::Identity( PhaseTransform *phase ) {
37  assert(in(1) != this, "Must already have called Value");
38  assert(in(2) != this, "Must already have called Value");
39
40  // Remove double negation
41  const Type *zero = add_id();
42  if( phase->type( in(1) )->higher_equal( zero ) &&
43      in(2)->Opcode() == Opcode() &&
44      phase->type( in(2)->in(1) )->higher_equal( zero ) ) {
45    return in(2)->in(2);
46  }
47
48  // Convert "(X+Y) - Y" into X and "(X+Y) - X" into Y
49  if( in(1)->Opcode() == Op_AddI ) {
50    if( phase->eqv(in(1)->in(2),in(2)) )
51      return in(1)->in(1);
52    if (phase->eqv(in(1)->in(1),in(2)))
53      return in(1)->in(2);
54
55    // Also catch: "(X + Opaque2(Y)) - Y".  In this case, 'Y' is a loop-varying
56    // trip counter and X is likely to be loop-invariant (that's how O2 Nodes
57    // are originally used, although the optimizer sometimes jiggers things).
58    // This folding through an O2 removes a loop-exit use of a loop-varying
59    // value and generally lowers register pressure in and around the loop.
60    if( in(1)->in(2)->Opcode() == Op_Opaque2 &&
61        phase->eqv(in(1)->in(2)->in(1),in(2)) )
62      return in(1)->in(1);
63  }
64
65  return ( phase->type( in(2) )->higher_equal( zero ) ) ? in(1) : this;
66}
67
68//------------------------------Value------------------------------------------
69// A subtract node differences it's two inputs.
70const Type *SubNode::Value( PhaseTransform *phase ) const {
71  const Node* in1 = in(1);
72  const Node* in2 = in(2);
73  // Either input is TOP ==> the result is TOP
74  const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
75  if( t1 == Type::TOP ) return Type::TOP;
76  const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
77  if( t2 == Type::TOP ) return Type::TOP;
78
79  // Not correct for SubFnode and AddFNode (must check for infinity)
80  // Equal?  Subtract is zero
81  if (phase->eqv_uncast(in1, in2))  return add_id();
82
83  // Either input is BOTTOM ==> the result is the local BOTTOM
84  if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
85    return bottom_type();
86
87  return sub(t1,t2);            // Local flavor of type subtraction
88
89}
90
91//=============================================================================
92
93//------------------------------Helper function--------------------------------
94static bool ok_to_convert(Node* inc, Node* iv) {
95    // Do not collapse (x+c0)-y if "+" is a loop increment, because the
96    // "-" is loop invariant and collapsing extends the live-range of "x"
97    // to overlap with the "+", forcing another register to be used in
98    // the loop.
99    // This test will be clearer with '&&' (apply DeMorgan's rule)
100    // but I like the early cutouts that happen here.
101    const PhiNode *phi;
102    if( ( !inc->in(1)->is_Phi() ||
103          !(phi=inc->in(1)->as_Phi()) ||
104          phi->is_copy() ||
105          !phi->region()->is_CountedLoop() ||
106          inc != phi->region()->as_CountedLoop()->incr() )
107       &&
108        // Do not collapse (x+c0)-iv if "iv" is a loop induction variable,
109        // because "x" maybe invariant.
110        ( !iv->is_loop_iv() )
111      ) {
112      return true;
113    } else {
114      return false;
115    }
116}
117//------------------------------Ideal------------------------------------------
118Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){
119  Node *in1 = in(1);
120  Node *in2 = in(2);
121  uint op1 = in1->Opcode();
122  uint op2 = in2->Opcode();
123
124#ifdef ASSERT
125  // Check for dead loop
126  if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
127      ( op1 == Op_AddI || op1 == Op_SubI ) &&
128      ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
129        phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1 ) ) )
130    assert(false, "dead loop in SubINode::Ideal");
131#endif
132
133  const Type *t2 = phase->type( in2 );
134  if( t2 == Type::TOP ) return NULL;
135  // Convert "x-c0" into "x+ -c0".
136  if( t2->base() == Type::Int ){        // Might be bottom or top...
137    const TypeInt *i = t2->is_int();
138    if( i->is_con() )
139      return new (phase->C, 3) AddINode(in1, phase->intcon(-i->get_con()));
140  }
141
142  // Convert "(x+c0) - y" into (x-y) + c0"
143  // Do not collapse (x+c0)-y if "+" is a loop increment or
144  // if "y" is a loop induction variable.
145  if( op1 == Op_AddI && ok_to_convert(in1, in2) ) {
146    const Type *tadd = phase->type( in1->in(2) );
147    if( tadd->singleton() && tadd != Type::TOP ) {
148      Node *sub2 = phase->transform( new (phase->C, 3) SubINode( in1->in(1), in2 ));
149      return new (phase->C, 3) AddINode( sub2, in1->in(2) );
150    }
151  }
152
153
154  // Convert "x - (y+c0)" into "(x-y) - c0"
155  // Need the same check as in above optimization but reversed.
156  if (op2 == Op_AddI && ok_to_convert(in2, in1)) {
157    Node* in21 = in2->in(1);
158    Node* in22 = in2->in(2);
159    const TypeInt* tcon = phase->type(in22)->isa_int();
160    if (tcon != NULL && tcon->is_con()) {
161      Node* sub2 = phase->transform( new (phase->C, 3) SubINode(in1, in21) );
162      Node* neg_c0 = phase->intcon(- tcon->get_con());
163      return new (phase->C, 3) AddINode(sub2, neg_c0);
164    }
165  }
166
167  const Type *t1 = phase->type( in1 );
168  if( t1 == Type::TOP ) return NULL;
169
170#ifdef ASSERT
171  // Check for dead loop
172  if( ( op2 == Op_AddI || op2 == Op_SubI ) &&
173      ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
174        phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
175    assert(false, "dead loop in SubINode::Ideal");
176#endif
177
178  // Convert "x - (x+y)" into "-y"
179  if( op2 == Op_AddI &&
180      phase->eqv( in1, in2->in(1) ) )
181    return new (phase->C, 3) SubINode( phase->intcon(0),in2->in(2));
182  // Convert "(x-y) - x" into "-y"
183  if( op1 == Op_SubI &&
184      phase->eqv( in1->in(1), in2 ) )
185    return new (phase->C, 3) SubINode( phase->intcon(0),in1->in(2));
186  // Convert "x - (y+x)" into "-y"
187  if( op2 == Op_AddI &&
188      phase->eqv( in1, in2->in(2) ) )
189    return new (phase->C, 3) SubINode( phase->intcon(0),in2->in(1));
190
191  // Convert "0 - (x-y)" into "y-x"
192  if( t1 == TypeInt::ZERO && op2 == Op_SubI )
193    return new (phase->C, 3) SubINode( in2->in(2), in2->in(1) );
194
195  // Convert "0 - (x+con)" into "-con-x"
196  jint con;
197  if( t1 == TypeInt::ZERO && op2 == Op_AddI &&
198      (con = in2->in(2)->find_int_con(0)) != 0 )
199    return new (phase->C, 3) SubINode( phase->intcon(-con), in2->in(1) );
200
201  // Convert "(X+A) - (X+B)" into "A - B"
202  if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) )
203    return new (phase->C, 3) SubINode( in1->in(2), in2->in(2) );
204
205  // Convert "(A+X) - (B+X)" into "A - B"
206  if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) )
207    return new (phase->C, 3) SubINode( in1->in(1), in2->in(1) );
208
209  // Convert "(A+X) - (X+B)" into "A - B"
210  if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(1) )
211    return new (phase->C, 3) SubINode( in1->in(1), in2->in(2) );
212
213  // Convert "(X+A) - (B+X)" into "A - B"
214  if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(2) )
215    return new (phase->C, 3) SubINode( in1->in(2), in2->in(1) );
216
217  // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally
218  // nicer to optimize than subtract.
219  if( op2 == Op_SubI && in2->outcnt() == 1) {
220    Node *add1 = phase->transform( new (phase->C, 3) AddINode( in1, in2->in(2) ) );
221    return new (phase->C, 3) SubINode( add1, in2->in(1) );
222  }
223
224  return NULL;
225}
226
227//------------------------------sub--------------------------------------------
228// A subtract node differences it's two inputs.
229const Type *SubINode::sub( const Type *t1, const Type *t2 ) const {
230  const TypeInt *r0 = t1->is_int(); // Handy access
231  const TypeInt *r1 = t2->is_int();
232  int32 lo = r0->_lo - r1->_hi;
233  int32 hi = r0->_hi - r1->_lo;
234
235  // We next check for 32-bit overflow.
236  // If that happens, we just assume all integers are possible.
237  if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
238       ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
239      (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
240       ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
241    return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen));
242  else                          // Overflow; assume all integers
243    return TypeInt::INT;
244}
245
246//=============================================================================
247//------------------------------Ideal------------------------------------------
248Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
249  Node *in1 = in(1);
250  Node *in2 = in(2);
251  uint op1 = in1->Opcode();
252  uint op2 = in2->Opcode();
253
254#ifdef ASSERT
255  // Check for dead loop
256  if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
257      ( op1 == Op_AddL || op1 == Op_SubL ) &&
258      ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
259        phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1  ) ) )
260    assert(false, "dead loop in SubLNode::Ideal");
261#endif
262
263  if( phase->type( in2 ) == Type::TOP ) return NULL;
264  const TypeLong *i = phase->type( in2 )->isa_long();
265  // Convert "x-c0" into "x+ -c0".
266  if( i &&                      // Might be bottom or top...
267      i->is_con() )
268    return new (phase->C, 3) AddLNode(in1, phase->longcon(-i->get_con()));
269
270  // Convert "(x+c0) - y" into (x-y) + c0"
271  // Do not collapse (x+c0)-y if "+" is a loop increment or
272  // if "y" is a loop induction variable.
273  if( op1 == Op_AddL && ok_to_convert(in1, in2) ) {
274    Node *in11 = in1->in(1);
275    const Type *tadd = phase->type( in1->in(2) );
276    if( tadd->singleton() && tadd != Type::TOP ) {
277      Node *sub2 = phase->transform( new (phase->C, 3) SubLNode( in11, in2 ));
278      return new (phase->C, 3) AddLNode( sub2, in1->in(2) );
279    }
280  }
281
282  // Convert "x - (y+c0)" into "(x-y) - c0"
283  // Need the same check as in above optimization but reversed.
284  if (op2 == Op_AddL && ok_to_convert(in2, in1)) {
285    Node* in21 = in2->in(1);
286    Node* in22 = in2->in(2);
287    const TypeLong* tcon = phase->type(in22)->isa_long();
288    if (tcon != NULL && tcon->is_con()) {
289      Node* sub2 = phase->transform( new (phase->C, 3) SubLNode(in1, in21) );
290      Node* neg_c0 = phase->longcon(- tcon->get_con());
291      return new (phase->C, 3) AddLNode(sub2, neg_c0);
292    }
293  }
294
295  const Type *t1 = phase->type( in1 );
296  if( t1 == Type::TOP ) return NULL;
297
298#ifdef ASSERT
299  // Check for dead loop
300  if( ( op2 == Op_AddL || op2 == Op_SubL ) &&
301      ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
302        phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
303    assert(false, "dead loop in SubLNode::Ideal");
304#endif
305
306  // Convert "x - (x+y)" into "-y"
307  if( op2 == Op_AddL &&
308      phase->eqv( in1, in2->in(1) ) )
309    return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO), in2->in(2));
310  // Convert "x - (y+x)" into "-y"
311  if( op2 == Op_AddL &&
312      phase->eqv( in1, in2->in(2) ) )
313    return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO),in2->in(1));
314
315  // Convert "0 - (x-y)" into "y-x"
316  if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL )
317    return new (phase->C, 3) SubLNode( in2->in(2), in2->in(1) );
318
319  // Convert "(X+A) - (X+B)" into "A - B"
320  if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) )
321    return new (phase->C, 3) SubLNode( in1->in(2), in2->in(2) );
322
323  // Convert "(A+X) - (B+X)" into "A - B"
324  if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) )
325    return new (phase->C, 3) SubLNode( in1->in(1), in2->in(1) );
326
327  // Convert "A-(B-C)" into (A+C)-B"
328  if( op2 == Op_SubL && in2->outcnt() == 1) {
329    Node *add1 = phase->transform( new (phase->C, 3) AddLNode( in1, in2->in(2) ) );
330    return new (phase->C, 3) SubLNode( add1, in2->in(1) );
331  }
332
333  return NULL;
334}
335
336//------------------------------sub--------------------------------------------
337// A subtract node differences it's two inputs.
338const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const {
339  const TypeLong *r0 = t1->is_long(); // Handy access
340  const TypeLong *r1 = t2->is_long();
341  jlong lo = r0->_lo - r1->_hi;
342  jlong hi = r0->_hi - r1->_lo;
343
344  // We next check for 32-bit overflow.
345  // If that happens, we just assume all integers are possible.
346  if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
347       ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
348      (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
349       ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
350    return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen));
351  else                          // Overflow; assume all integers
352    return TypeLong::LONG;
353}
354
355//=============================================================================
356//------------------------------Value------------------------------------------
357// A subtract node differences its two inputs.
358const Type *SubFPNode::Value( PhaseTransform *phase ) const {
359  const Node* in1 = in(1);
360  const Node* in2 = in(2);
361  // Either input is TOP ==> the result is TOP
362  const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
363  if( t1 == Type::TOP ) return Type::TOP;
364  const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
365  if( t2 == Type::TOP ) return Type::TOP;
366
367  // if both operands are infinity of same sign, the result is NaN; do
368  // not replace with zero
369  if( (t1->is_finite() && t2->is_finite()) ) {
370    if( phase->eqv(in1, in2) ) return add_id();
371  }
372
373  // Either input is BOTTOM ==> the result is the local BOTTOM
374  const Type *bot = bottom_type();
375  if( (t1 == bot) || (t2 == bot) ||
376      (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
377    return bot;
378
379  return sub(t1,t2);            // Local flavor of type subtraction
380}
381
382
383//=============================================================================
384//------------------------------Ideal------------------------------------------
385Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
386  const Type *t2 = phase->type( in(2) );
387  // Convert "x-c0" into "x+ -c0".
388  if( t2->base() == Type::FloatCon ) {  // Might be bottom or top...
389    // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) );
390  }
391
392  // Not associative because of boundary conditions (infinity)
393  if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
394    // Convert "x - (x+y)" into "-y"
395    if( in(2)->is_Add() &&
396        phase->eqv(in(1),in(2)->in(1) ) )
397      return new (phase->C, 3) SubFNode( phase->makecon(TypeF::ZERO),in(2)->in(2));
398  }
399
400  // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes
401  // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0.
402  //if( phase->type(in(1)) == TypeF::ZERO )
403  //return new (phase->C, 2) NegFNode(in(2));
404
405  return NULL;
406}
407
408//------------------------------sub--------------------------------------------
409// A subtract node differences its two inputs.
410const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const {
411  // no folding if one of operands is infinity or NaN, do not do constant folding
412  if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) {
413    return TypeF::make( t1->getf() - t2->getf() );
414  }
415  else if( g_isnan(t1->getf()) ) {
416    return t1;
417  }
418  else if( g_isnan(t2->getf()) ) {
419    return t2;
420  }
421  else {
422    return Type::FLOAT;
423  }
424}
425
426//=============================================================================
427//------------------------------Ideal------------------------------------------
428Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){
429  const Type *t2 = phase->type( in(2) );
430  // Convert "x-c0" into "x+ -c0".
431  if( t2->base() == Type::DoubleCon ) { // Might be bottom or top...
432    // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) );
433  }
434
435  // Not associative because of boundary conditions (infinity)
436  if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
437    // Convert "x - (x+y)" into "-y"
438    if( in(2)->is_Add() &&
439        phase->eqv(in(1),in(2)->in(1) ) )
440      return new (phase->C, 3) SubDNode( phase->makecon(TypeD::ZERO),in(2)->in(2));
441  }
442
443  // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes
444  // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0.
445  //if( phase->type(in(1)) == TypeD::ZERO )
446  //return new (phase->C, 2) NegDNode(in(2));
447
448  return NULL;
449}
450
451//------------------------------sub--------------------------------------------
452// A subtract node differences its two inputs.
453const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const {
454  // no folding if one of operands is infinity or NaN, do not do constant folding
455  if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) {
456    return TypeD::make( t1->getd() - t2->getd() );
457  }
458  else if( g_isnan(t1->getd()) ) {
459    return t1;
460  }
461  else if( g_isnan(t2->getd()) ) {
462    return t2;
463  }
464  else {
465    return Type::DOUBLE;
466  }
467}
468
469//=============================================================================
470//------------------------------Idealize---------------------------------------
471// Unlike SubNodes, compare must still flatten return value to the
472// range -1, 0, 1.
473// And optimizations like those for (X + Y) - X fail if overflow happens.
474Node *CmpNode::Identity( PhaseTransform *phase ) {
475  return this;
476}
477
478//=============================================================================
479//------------------------------cmp--------------------------------------------
480// Simplify a CmpI (compare 2 integers) node, based on local information.
481// If both inputs are constants, compare them.
482const Type *CmpINode::sub( const Type *t1, const Type *t2 ) const {
483  const TypeInt *r0 = t1->is_int(); // Handy access
484  const TypeInt *r1 = t2->is_int();
485
486  if( r0->_hi < r1->_lo )       // Range is always low?
487    return TypeInt::CC_LT;
488  else if( r0->_lo > r1->_hi )  // Range is always high?
489    return TypeInt::CC_GT;
490
491  else if( r0->is_con() && r1->is_con() ) { // comparing constants?
492    assert(r0->get_con() == r1->get_con(), "must be equal");
493    return TypeInt::CC_EQ;      // Equal results.
494  } else if( r0->_hi == r1->_lo ) // Range is never high?
495    return TypeInt::CC_LE;
496  else if( r0->_lo == r1->_hi ) // Range is never low?
497    return TypeInt::CC_GE;
498  return TypeInt::CC;           // else use worst case results
499}
500
501// Simplify a CmpU (compare 2 integers) node, based on local information.
502// If both inputs are constants, compare them.
503const Type *CmpUNode::sub( const Type *t1, const Type *t2 ) const {
504  assert(!t1->isa_ptr(), "obsolete usage of CmpU");
505
506  // comparing two unsigned ints
507  const TypeInt *r0 = t1->is_int();   // Handy access
508  const TypeInt *r1 = t2->is_int();
509
510  // Current installed version
511  // Compare ranges for non-overlap
512  juint lo0 = r0->_lo;
513  juint hi0 = r0->_hi;
514  juint lo1 = r1->_lo;
515  juint hi1 = r1->_hi;
516
517  // If either one has both negative and positive values,
518  // it therefore contains both 0 and -1, and since [0..-1] is the
519  // full unsigned range, the type must act as an unsigned bottom.
520  bool bot0 = ((jint)(lo0 ^ hi0) < 0);
521  bool bot1 = ((jint)(lo1 ^ hi1) < 0);
522
523  if (bot0 || bot1) {
524    // All unsigned values are LE -1 and GE 0.
525    if (lo0 == 0 && hi0 == 0) {
526      return TypeInt::CC_LE;            //   0 <= bot
527    } else if (lo1 == 0 && hi1 == 0) {
528      return TypeInt::CC_GE;            // bot >= 0
529    }
530  } else {
531    // We can use ranges of the form [lo..hi] if signs are the same.
532    assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
533    // results are reversed, '-' > '+' for unsigned compare
534    if (hi0 < lo1) {
535      return TypeInt::CC_LT;            // smaller
536    } else if (lo0 > hi1) {
537      return TypeInt::CC_GT;            // greater
538    } else if (hi0 == lo1 && lo0 == hi1) {
539      return TypeInt::CC_EQ;            // Equal results
540    } else if (lo0 >= hi1) {
541      return TypeInt::CC_GE;
542    } else if (hi0 <= lo1) {
543      // Check for special case in Hashtable::get.  (See below.)
544      if ((jint)lo0 >= 0 && (jint)lo1 >= 0 &&
545          in(1)->Opcode() == Op_ModI &&
546          in(1)->in(2) == in(2) )
547        return TypeInt::CC_LT;
548      return TypeInt::CC_LE;
549    }
550  }
551  // Check for special case in Hashtable::get - the hash index is
552  // mod'ed to the table size so the following range check is useless.
553  // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have
554  // to be positive.
555  // (This is a gross hack, since the sub method never
556  // looks at the structure of the node in any other case.)
557  if ((jint)lo0 >= 0 && (jint)lo1 >= 0 &&
558      in(1)->Opcode() == Op_ModI &&
559      in(1)->in(2)->uncast() == in(2)->uncast())
560    return TypeInt::CC_LT;
561  return TypeInt::CC;                   // else use worst case results
562}
563
564//------------------------------Idealize---------------------------------------
565Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
566  if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
567    switch (in(1)->Opcode()) {
568    case Op_CmpL3:              // Collapse a CmpL3/CmpI into a CmpL
569      return new (phase->C, 3) CmpLNode(in(1)->in(1),in(1)->in(2));
570    case Op_CmpF3:              // Collapse a CmpF3/CmpI into a CmpF
571      return new (phase->C, 3) CmpFNode(in(1)->in(1),in(1)->in(2));
572    case Op_CmpD3:              // Collapse a CmpD3/CmpI into a CmpD
573      return new (phase->C, 3) CmpDNode(in(1)->in(1),in(1)->in(2));
574    //case Op_SubI:
575      // If (x - y) cannot overflow, then ((x - y) <?> 0)
576      // can be turned into (x <?> y).
577      // This is handled (with more general cases) by Ideal_sub_algebra.
578    }
579  }
580  return NULL;                  // No change
581}
582
583
584//=============================================================================
585// Simplify a CmpL (compare 2 longs ) node, based on local information.
586// If both inputs are constants, compare them.
587const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
588  const TypeLong *r0 = t1->is_long(); // Handy access
589  const TypeLong *r1 = t2->is_long();
590
591  if( r0->_hi < r1->_lo )       // Range is always low?
592    return TypeInt::CC_LT;
593  else if( r0->_lo > r1->_hi )  // Range is always high?
594    return TypeInt::CC_GT;
595
596  else if( r0->is_con() && r1->is_con() ) { // comparing constants?
597    assert(r0->get_con() == r1->get_con(), "must be equal");
598    return TypeInt::CC_EQ;      // Equal results.
599  } else if( r0->_hi == r1->_lo ) // Range is never high?
600    return TypeInt::CC_LE;
601  else if( r0->_lo == r1->_hi ) // Range is never low?
602    return TypeInt::CC_GE;
603  return TypeInt::CC;           // else use worst case results
604}
605
606//=============================================================================
607//------------------------------sub--------------------------------------------
608// Simplify an CmpP (compare 2 pointers) node, based on local information.
609// If both inputs are constants, compare them.
610const Type *CmpPNode::sub( const Type *t1, const Type *t2 ) const {
611  const TypePtr *r0 = t1->is_ptr(); // Handy access
612  const TypePtr *r1 = t2->is_ptr();
613
614  // Undefined inputs makes for an undefined result
615  if( TypePtr::above_centerline(r0->_ptr) ||
616      TypePtr::above_centerline(r1->_ptr) )
617    return Type::TOP;
618
619  if (r0 == r1 && r0->singleton()) {
620    // Equal pointer constants (klasses, nulls, etc.)
621    return TypeInt::CC_EQ;
622  }
623
624  // See if it is 2 unrelated classes.
625  const TypeOopPtr* p0 = r0->isa_oopptr();
626  const TypeOopPtr* p1 = r1->isa_oopptr();
627  if (p0 && p1) {
628    Node* in1 = in(1)->uncast();
629    Node* in2 = in(2)->uncast();
630    AllocateNode* alloc1 = AllocateNode::Ideal_allocation(in1, NULL);
631    AllocateNode* alloc2 = AllocateNode::Ideal_allocation(in2, NULL);
632    if (MemNode::detect_ptr_independence(in1, alloc1, in2, alloc2, NULL)) {
633      return TypeInt::CC_GT;  // different pointers
634    }
635    ciKlass* klass0 = p0->klass();
636    bool    xklass0 = p0->klass_is_exact();
637    ciKlass* klass1 = p1->klass();
638    bool    xklass1 = p1->klass_is_exact();
639    int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
640    if (klass0 && klass1 &&
641        kps != 1 &&             // both or neither are klass pointers
642        klass0->is_loaded() && !klass0->is_interface() && // do not trust interfaces
643        klass1->is_loaded() && !klass1->is_interface() &&
644        (!klass0->is_obj_array_klass() ||
645         !klass0->as_obj_array_klass()->base_element_klass()->is_interface()) &&
646        (!klass1->is_obj_array_klass() ||
647         !klass1->as_obj_array_klass()->base_element_klass()->is_interface())) {
648      bool unrelated_classes = false;
649      // See if neither subclasses the other, or if the class on top
650      // is precise.  In either of these cases, the compare is known
651      // to fail if at least one of the pointers is provably not null.
652      if (klass0->equals(klass1)   ||   // if types are unequal but klasses are
653          !klass0->is_java_klass() ||   // types not part of Java language?
654          !klass1->is_java_klass()) {   // types not part of Java language?
655        // Do nothing; we know nothing for imprecise types
656      } else if (klass0->is_subtype_of(klass1)) {
657        // If klass1's type is PRECISE, then classes are unrelated.
658        unrelated_classes = xklass1;
659      } else if (klass1->is_subtype_of(klass0)) {
660        // If klass0's type is PRECISE, then classes are unrelated.
661        unrelated_classes = xklass0;
662      } else {                  // Neither subtypes the other
663        unrelated_classes = true;
664      }
665      if (unrelated_classes) {
666        // The oops classes are known to be unrelated. If the joined PTRs of
667        // two oops is not Null and not Bottom, then we are sure that one
668        // of the two oops is non-null, and the comparison will always fail.
669        TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
670        if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
671          return TypeInt::CC_GT;
672        }
673      }
674    }
675  }
676
677  // Known constants can be compared exactly
678  // Null can be distinguished from any NotNull pointers
679  // Unknown inputs makes an unknown result
680  if( r0->singleton() ) {
681    intptr_t bits0 = r0->get_con();
682    if( r1->singleton() )
683      return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
684    return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
685  } else if( r1->singleton() ) {
686    intptr_t bits1 = r1->get_con();
687    return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
688  } else
689    return TypeInt::CC;
690}
691
692//------------------------------Ideal------------------------------------------
693// Check for the case of comparing an unknown klass loaded from the primary
694// super-type array vs a known klass with no subtypes.  This amounts to
695// checking to see an unknown klass subtypes a known klass with no subtypes;
696// this only happens on an exact match.  We can shorten this test by 1 load.
697Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
698  // Constant pointer on right?
699  const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
700  if (t2 == NULL || !t2->klass_is_exact())
701    return NULL;
702  // Get the constant klass we are comparing to.
703  ciKlass* superklass = t2->klass();
704
705  // Now check for LoadKlass on left.
706  Node* ldk1 = in(1);
707  if (ldk1->is_DecodeN()) {
708    ldk1 = ldk1->in(1);
709    if (ldk1->Opcode() != Op_LoadNKlass )
710      return NULL;
711  } else if (ldk1->Opcode() != Op_LoadKlass )
712    return NULL;
713  // Take apart the address of the LoadKlass:
714  Node* adr1 = ldk1->in(MemNode::Address);
715  intptr_t con2 = 0;
716  Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2);
717  if (ldk2 == NULL)
718    return NULL;
719  if (con2 == oopDesc::klass_offset_in_bytes()) {
720    // We are inspecting an object's concrete class.
721    // Short-circuit the check if the query is abstract.
722    if (superklass->is_interface() ||
723        superklass->is_abstract()) {
724      // Make it come out always false:
725      this->set_req(2, phase->makecon(TypePtr::NULL_PTR));
726      return this;
727    }
728  }
729
730  // Check for a LoadKlass from primary supertype array.
731  // Any nested loadklass from loadklass+con must be from the p.s. array.
732  if (ldk2->is_DecodeN()) {
733    // Keep ldk2 as DecodeN since it could be used in CmpP below.
734    if (ldk2->in(1)->Opcode() != Op_LoadNKlass )
735      return NULL;
736  } else if (ldk2->Opcode() != Op_LoadKlass)
737    return NULL;
738
739  // Verify that we understand the situation
740  if (con2 != (intptr_t) superklass->super_check_offset())
741    return NULL;                // Might be element-klass loading from array klass
742
743  // If 'superklass' has no subklasses and is not an interface, then we are
744  // assured that the only input which will pass the type check is
745  // 'superklass' itself.
746  //
747  // We could be more liberal here, and allow the optimization on interfaces
748  // which have a single implementor.  This would require us to increase the
749  // expressiveness of the add_dependency() mechanism.
750  // %%% Do this after we fix TypeOopPtr:  Deps are expressive enough now.
751
752  // Object arrays must have their base element have no subtypes
753  while (superklass->is_obj_array_klass()) {
754    ciType* elem = superklass->as_obj_array_klass()->element_type();
755    superklass = elem->as_klass();
756  }
757  if (superklass->is_instance_klass()) {
758    ciInstanceKlass* ik = superklass->as_instance_klass();
759    if (ik->has_subklass() || ik->is_interface())  return NULL;
760    // Add a dependency if there is a chance that a subclass will be added later.
761    if (!ik->is_final()) {
762      phase->C->dependencies()->assert_leaf_type(ik);
763    }
764  }
765
766  // Bypass the dependent load, and compare directly
767  this->set_req(1,ldk2);
768
769  return this;
770}
771
772//=============================================================================
773//------------------------------sub--------------------------------------------
774// Simplify an CmpN (compare 2 pointers) node, based on local information.
775// If both inputs are constants, compare them.
776const Type *CmpNNode::sub( const Type *t1, const Type *t2 ) const {
777  const TypePtr *r0 = t1->make_ptr(); // Handy access
778  const TypePtr *r1 = t2->make_ptr();
779
780  // Undefined inputs makes for an undefined result
781  if( TypePtr::above_centerline(r0->_ptr) ||
782      TypePtr::above_centerline(r1->_ptr) )
783    return Type::TOP;
784
785  if (r0 == r1 && r0->singleton()) {
786    // Equal pointer constants (klasses, nulls, etc.)
787    return TypeInt::CC_EQ;
788  }
789
790  // See if it is 2 unrelated classes.
791  const TypeOopPtr* p0 = r0->isa_oopptr();
792  const TypeOopPtr* p1 = r1->isa_oopptr();
793  if (p0 && p1) {
794    ciKlass* klass0 = p0->klass();
795    bool    xklass0 = p0->klass_is_exact();
796    ciKlass* klass1 = p1->klass();
797    bool    xklass1 = p1->klass_is_exact();
798    int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
799    if (klass0 && klass1 &&
800        kps != 1 &&             // both or neither are klass pointers
801        !klass0->is_interface() && // do not trust interfaces
802        !klass1->is_interface()) {
803      bool unrelated_classes = false;
804      // See if neither subclasses the other, or if the class on top
805      // is precise.  In either of these cases, the compare is known
806      // to fail if at least one of the pointers is provably not null.
807      if (klass0->equals(klass1)   ||   // if types are unequal but klasses are
808          !klass0->is_java_klass() ||   // types not part of Java language?
809          !klass1->is_java_klass()) {   // types not part of Java language?
810        // Do nothing; we know nothing for imprecise types
811      } else if (klass0->is_subtype_of(klass1)) {
812        // If klass1's type is PRECISE, then classes are unrelated.
813        unrelated_classes = xklass1;
814      } else if (klass1->is_subtype_of(klass0)) {
815        // If klass0's type is PRECISE, then classes are unrelated.
816        unrelated_classes = xklass0;
817      } else {                  // Neither subtypes the other
818        unrelated_classes = true;
819      }
820      if (unrelated_classes) {
821        // The oops classes are known to be unrelated. If the joined PTRs of
822        // two oops is not Null and not Bottom, then we are sure that one
823        // of the two oops is non-null, and the comparison will always fail.
824        TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
825        if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
826          return TypeInt::CC_GT;
827        }
828      }
829    }
830  }
831
832  // Known constants can be compared exactly
833  // Null can be distinguished from any NotNull pointers
834  // Unknown inputs makes an unknown result
835  if( r0->singleton() ) {
836    intptr_t bits0 = r0->get_con();
837    if( r1->singleton() )
838      return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
839    return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
840  } else if( r1->singleton() ) {
841    intptr_t bits1 = r1->get_con();
842    return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
843  } else
844    return TypeInt::CC;
845}
846
847//------------------------------Ideal------------------------------------------
848Node *CmpNNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
849  return NULL;
850}
851
852//=============================================================================
853//------------------------------Value------------------------------------------
854// Simplify an CmpF (compare 2 floats ) node, based on local information.
855// If both inputs are constants, compare them.
856const Type *CmpFNode::Value( PhaseTransform *phase ) const {
857  const Node* in1 = in(1);
858  const Node* in2 = in(2);
859  // Either input is TOP ==> the result is TOP
860  const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
861  if( t1 == Type::TOP ) return Type::TOP;
862  const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
863  if( t2 == Type::TOP ) return Type::TOP;
864
865  // Not constants?  Don't know squat - even if they are the same
866  // value!  If they are NaN's they compare to LT instead of EQ.
867  const TypeF *tf1 = t1->isa_float_constant();
868  const TypeF *tf2 = t2->isa_float_constant();
869  if( !tf1 || !tf2 ) return TypeInt::CC;
870
871  // This implements the Java bytecode fcmpl, so unordered returns -1.
872  if( tf1->is_nan() || tf2->is_nan() )
873    return TypeInt::CC_LT;
874
875  if( tf1->_f < tf2->_f ) return TypeInt::CC_LT;
876  if( tf1->_f > tf2->_f ) return TypeInt::CC_GT;
877  assert( tf1->_f == tf2->_f, "do not understand FP behavior" );
878  return TypeInt::CC_EQ;
879}
880
881
882//=============================================================================
883//------------------------------Value------------------------------------------
884// Simplify an CmpD (compare 2 doubles ) node, based on local information.
885// If both inputs are constants, compare them.
886const Type *CmpDNode::Value( PhaseTransform *phase ) const {
887  const Node* in1 = in(1);
888  const Node* in2 = in(2);
889  // Either input is TOP ==> the result is TOP
890  const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
891  if( t1 == Type::TOP ) return Type::TOP;
892  const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
893  if( t2 == Type::TOP ) return Type::TOP;
894
895  // Not constants?  Don't know squat - even if they are the same
896  // value!  If they are NaN's they compare to LT instead of EQ.
897  const TypeD *td1 = t1->isa_double_constant();
898  const TypeD *td2 = t2->isa_double_constant();
899  if( !td1 || !td2 ) return TypeInt::CC;
900
901  // This implements the Java bytecode dcmpl, so unordered returns -1.
902  if( td1->is_nan() || td2->is_nan() )
903    return TypeInt::CC_LT;
904
905  if( td1->_d < td2->_d ) return TypeInt::CC_LT;
906  if( td1->_d > td2->_d ) return TypeInt::CC_GT;
907  assert( td1->_d == td2->_d, "do not understand FP behavior" );
908  return TypeInt::CC_EQ;
909}
910
911//------------------------------Ideal------------------------------------------
912Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){
913  // Check if we can change this to a CmpF and remove a ConvD2F operation.
914  // Change  (CMPD (F2D (float)) (ConD value))
915  // To      (CMPF      (float)  (ConF value))
916  // Valid when 'value' does not lose precision as a float.
917  // Benefits: eliminates conversion, does not require 24-bit mode
918
919  // NaNs prevent commuting operands.  This transform works regardless of the
920  // order of ConD and ConvF2D inputs by preserving the original order.
921  int idx_f2d = 1;              // ConvF2D on left side?
922  if( in(idx_f2d)->Opcode() != Op_ConvF2D )
923    idx_f2d = 2;                // No, swap to check for reversed args
924  int idx_con = 3-idx_f2d;      // Check for the constant on other input
925
926  if( ConvertCmpD2CmpF &&
927      in(idx_f2d)->Opcode() == Op_ConvF2D &&
928      in(idx_con)->Opcode() == Op_ConD ) {
929    const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
930    double t2_value_as_double = t2->_d;
931    float  t2_value_as_float  = (float)t2_value_as_double;
932    if( t2_value_as_double == (double)t2_value_as_float ) {
933      // Test value can be represented as a float
934      // Eliminate the conversion to double and create new comparison
935      Node *new_in1 = in(idx_f2d)->in(1);
936      Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
937      if( idx_f2d != 1 ) {      // Must flip args to match original order
938        Node *tmp = new_in1;
939        new_in1 = new_in2;
940        new_in2 = tmp;
941      }
942      CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
943        ? new (phase->C, 3) CmpF3Node( new_in1, new_in2 )
944        : new (phase->C, 3) CmpFNode ( new_in1, new_in2 ) ;
945      return new_cmp;           // Changed to CmpFNode
946    }
947    // Testing value required the precision of a double
948  }
949  return NULL;                  // No change
950}
951
952
953//=============================================================================
954//------------------------------cc2logical-------------------------------------
955// Convert a condition code type to a logical type
956const Type *BoolTest::cc2logical( const Type *CC ) const {
957  if( CC == Type::TOP ) return Type::TOP;
958  if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
959  const TypeInt *ti = CC->is_int();
960  if( ti->is_con() ) {          // Only 1 kind of condition codes set?
961    // Match low order 2 bits
962    int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
963    if( _test & 4 ) tmp = 1-tmp;     // Optionally complement result
964    return TypeInt::make(tmp);       // Boolean result
965  }
966
967  if( CC == TypeInt::CC_GE ) {
968    if( _test == ge ) return TypeInt::ONE;
969    if( _test == lt ) return TypeInt::ZERO;
970  }
971  if( CC == TypeInt::CC_LE ) {
972    if( _test == le ) return TypeInt::ONE;
973    if( _test == gt ) return TypeInt::ZERO;
974  }
975
976  return TypeInt::BOOL;
977}
978
979//------------------------------dump_spec-------------------------------------
980// Print special per-node info
981#ifndef PRODUCT
982void BoolTest::dump_on(outputStream *st) const {
983  const char *msg[] = {"eq","gt","??","lt","ne","le","??","ge"};
984  st->print(msg[_test]);
985}
986#endif
987
988//=============================================================================
989uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); }
990uint BoolNode::size_of() const { return sizeof(BoolNode); }
991
992//------------------------------operator==-------------------------------------
993uint BoolNode::cmp( const Node &n ) const {
994  const BoolNode *b = (const BoolNode *)&n; // Cast up
995  return (_test._test == b->_test._test);
996}
997
998//------------------------------clone_cmp--------------------------------------
999// Clone a compare/bool tree
1000static Node *clone_cmp( Node *cmp, Node *cmp1, Node *cmp2, PhaseGVN *gvn, BoolTest::mask test ) {
1001  Node *ncmp = cmp->clone();
1002  ncmp->set_req(1,cmp1);
1003  ncmp->set_req(2,cmp2);
1004  ncmp = gvn->transform( ncmp );
1005  return new (gvn->C, 2) BoolNode( ncmp, test );
1006}
1007
1008//-------------------------------make_predicate--------------------------------
1009Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
1010  if (test_value->is_Con())   return test_value;
1011  if (test_value->is_Bool())  return test_value;
1012  Compile* C = phase->C;
1013  if (test_value->is_CMove() &&
1014      test_value->in(CMoveNode::Condition)->is_Bool()) {
1015    BoolNode*   bol   = test_value->in(CMoveNode::Condition)->as_Bool();
1016    const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
1017    const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
1018    if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
1019      return bol;
1020    } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
1021      return phase->transform( bol->negate(phase) );
1022    }
1023    // Else fall through.  The CMove gets in the way of the test.
1024    // It should be the case that make_predicate(bol->as_int_value()) == bol.
1025  }
1026  Node* cmp = new (C, 3) CmpINode(test_value, phase->intcon(0));
1027  cmp = phase->transform(cmp);
1028  Node* bol = new (C, 2) BoolNode(cmp, BoolTest::ne);
1029  return phase->transform(bol);
1030}
1031
1032//--------------------------------as_int_value---------------------------------
1033Node* BoolNode::as_int_value(PhaseGVN* phase) {
1034  // Inverse to make_predicate.  The CMove probably boils down to a Conv2B.
1035  Node* cmov = CMoveNode::make(phase->C, NULL, this,
1036                               phase->intcon(0), phase->intcon(1),
1037                               TypeInt::BOOL);
1038  return phase->transform(cmov);
1039}
1040
1041//----------------------------------negate-------------------------------------
1042BoolNode* BoolNode::negate(PhaseGVN* phase) {
1043  Compile* C = phase->C;
1044  return new (C, 2) BoolNode(in(1), _test.negate());
1045}
1046
1047
1048//------------------------------Ideal------------------------------------------
1049Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1050  // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
1051  // This moves the constant to the right.  Helps value-numbering.
1052  Node *cmp = in(1);
1053  if( !cmp->is_Sub() ) return NULL;
1054  int cop = cmp->Opcode();
1055  if( cop == Op_FastLock || cop == Op_FastUnlock ) return NULL;
1056  Node *cmp1 = cmp->in(1);
1057  Node *cmp2 = cmp->in(2);
1058  if( !cmp1 ) return NULL;
1059
1060  // Constant on left?
1061  Node *con = cmp1;
1062  uint op2 = cmp2->Opcode();
1063  // Move constants to the right of compare's to canonicalize.
1064  // Do not muck with Opaque1 nodes, as this indicates a loop
1065  // guard that cannot change shape.
1066  if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 &&
1067      // Because of NaN's, CmpD and CmpF are not commutative
1068      cop != Op_CmpD && cop != Op_CmpF &&
1069      // Protect against swapping inputs to a compare when it is used by a
1070      // counted loop exit, which requires maintaining the loop-limit as in(2)
1071      !is_counted_loop_exit_test() ) {
1072    // Ok, commute the constant to the right of the cmp node.
1073    // Clone the Node, getting a new Node of the same class
1074    cmp = cmp->clone();
1075    // Swap inputs to the clone
1076    cmp->swap_edges(1, 2);
1077    cmp = phase->transform( cmp );
1078    return new (phase->C, 2) BoolNode( cmp, _test.commute() );
1079  }
1080
1081  // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
1082  // The XOR-1 is an idiom used to flip the sense of a bool.  We flip the
1083  // test instead.
1084  int cmp1_op = cmp1->Opcode();
1085  const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
1086  if (cmp2_type == NULL)  return NULL;
1087  Node* j_xor = cmp1;
1088  if( cmp2_type == TypeInt::ZERO &&
1089      cmp1_op == Op_XorI &&
1090      j_xor->in(1) != j_xor &&          // An xor of itself is dead
1091      phase->type( j_xor->in(2) ) == TypeInt::ONE &&
1092      (_test._test == BoolTest::eq ||
1093       _test._test == BoolTest::ne) ) {
1094    Node *ncmp = phase->transform(new (phase->C, 3) CmpINode(j_xor->in(1),cmp2));
1095    return new (phase->C, 2) BoolNode( ncmp, _test.negate() );
1096  }
1097
1098  // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
1099  // This is a standard idiom for branching on a boolean value.
1100  Node *c2b = cmp1;
1101  if( cmp2_type == TypeInt::ZERO &&
1102      cmp1_op == Op_Conv2B &&
1103      (_test._test == BoolTest::eq ||
1104       _test._test == BoolTest::ne) ) {
1105    Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
1106       ? (Node*)new (phase->C, 3) CmpINode(c2b->in(1),cmp2)
1107       : (Node*)new (phase->C, 3) CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
1108    );
1109    return new (phase->C, 2) BoolNode( ncmp, _test._test );
1110  }
1111
1112  // Comparing a SubI against a zero is equal to comparing the SubI
1113  // arguments directly.  This only works for eq and ne comparisons
1114  // due to possible integer overflow.
1115  if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1116        (cop == Op_CmpI) &&
1117        (cmp1->Opcode() == Op_SubI) &&
1118        ( cmp2_type == TypeInt::ZERO ) ) {
1119    Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(1),cmp1->in(2)));
1120    return new (phase->C, 2) BoolNode( ncmp, _test._test );
1121  }
1122
1123  // Change (-A vs 0) into (A vs 0) by commuting the test.  Disallow in the
1124  // most general case because negating 0x80000000 does nothing.  Needed for
1125  // the CmpF3/SubI/CmpI idiom.
1126  if( cop == Op_CmpI &&
1127      cmp1->Opcode() == Op_SubI &&
1128      cmp2_type == TypeInt::ZERO &&
1129      phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
1130      phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
1131    Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(2),cmp2));
1132    return new (phase->C, 2) BoolNode( ncmp, _test.commute() );
1133  }
1134
1135  //  The transformation below is not valid for either signed or unsigned
1136  //  comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
1137  //  This transformation can be resurrected when we are able to
1138  //  make inferences about the range of values being subtracted from
1139  //  (or added to) relative to the wraparound point.
1140  //
1141  //    // Remove +/-1's if possible.
1142  //    // "X <= Y-1" becomes "X <  Y"
1143  //    // "X+1 <= Y" becomes "X <  Y"
1144  //    // "X <  Y+1" becomes "X <= Y"
1145  //    // "X-1 <  Y" becomes "X <= Y"
1146  //    // Do not this to compares off of the counted-loop-end.  These guys are
1147  //    // checking the trip counter and they want to use the post-incremented
1148  //    // counter.  If they use the PRE-incremented counter, then the counter has
1149  //    // to be incremented in a private block on a loop backedge.
1150  //    if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
1151  //      return NULL;
1152  //  #ifndef PRODUCT
1153  //    // Do not do this in a wash GVN pass during verification.
1154  //    // Gets triggered by too many simple optimizations to be bothered with
1155  //    // re-trying it again and again.
1156  //    if( !phase->allow_progress() ) return NULL;
1157  //  #endif
1158  //    // Not valid for unsigned compare because of corner cases in involving zero.
1159  //    // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an
1160  //    // exception in case X is 0 (because 0-1 turns into 4billion unsigned but
1161  //    // "0 <=u Y" is always true).
1162  //    if( cmp->Opcode() == Op_CmpU ) return NULL;
1163  //    int cmp2_op = cmp2->Opcode();
1164  //    if( _test._test == BoolTest::le ) {
1165  //      if( cmp1_op == Op_AddI &&
1166  //          phase->type( cmp1->in(2) ) == TypeInt::ONE )
1167  //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt );
1168  //      else if( cmp2_op == Op_AddI &&
1169  //         phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 )
1170  //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt );
1171  //    } else if( _test._test == BoolTest::lt ) {
1172  //      if( cmp1_op == Op_AddI &&
1173  //          phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 )
1174  //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le );
1175  //      else if( cmp2_op == Op_AddI &&
1176  //         phase->type( cmp2->in(2) ) == TypeInt::ONE )
1177  //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le );
1178  //    }
1179
1180  return NULL;
1181}
1182
1183//------------------------------Value------------------------------------------
1184// Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
1185// based on local information.   If the input is constant, do it.
1186const Type *BoolNode::Value( PhaseTransform *phase ) const {
1187  return _test.cc2logical( phase->type( in(1) ) );
1188}
1189
1190//------------------------------dump_spec--------------------------------------
1191// Dump special per-node info
1192#ifndef PRODUCT
1193void BoolNode::dump_spec(outputStream *st) const {
1194  st->print("[");
1195  _test.dump_on(st);
1196  st->print("]");
1197}
1198#endif
1199
1200//------------------------------is_counted_loop_exit_test--------------------------------------
1201// Returns true if node is used by a counted loop node.
1202bool BoolNode::is_counted_loop_exit_test() {
1203  for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
1204    Node* use = fast_out(i);
1205    if (use->is_CountedLoopEnd()) {
1206      return true;
1207    }
1208  }
1209  return false;
1210}
1211
1212//=============================================================================
1213//------------------------------NegNode----------------------------------------
1214Node *NegFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1215  if( in(1)->Opcode() == Op_SubF )
1216    return new (phase->C, 3) SubFNode( in(1)->in(2), in(1)->in(1) );
1217  return NULL;
1218}
1219
1220Node *NegDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1221  if( in(1)->Opcode() == Op_SubD )
1222    return new (phase->C, 3) SubDNode( in(1)->in(2), in(1)->in(1) );
1223  return NULL;
1224}
1225
1226
1227//=============================================================================
1228//------------------------------Value------------------------------------------
1229// Compute sqrt
1230const Type *SqrtDNode::Value( PhaseTransform *phase ) const {
1231  const Type *t1 = phase->type( in(1) );
1232  if( t1 == Type::TOP ) return Type::TOP;
1233  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1234  double d = t1->getd();
1235  if( d < 0.0 ) return Type::DOUBLE;
1236  return TypeD::make( sqrt( d ) );
1237}
1238
1239//=============================================================================
1240//------------------------------Value------------------------------------------
1241// Compute cos
1242const Type *CosDNode::Value( PhaseTransform *phase ) const {
1243  const Type *t1 = phase->type( in(1) );
1244  if( t1 == Type::TOP ) return Type::TOP;
1245  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1246  double d = t1->getd();
1247  if( d < 0.0 ) return Type::DOUBLE;
1248  return TypeD::make( SharedRuntime::dcos( d ) );
1249}
1250
1251//=============================================================================
1252//------------------------------Value------------------------------------------
1253// Compute sin
1254const Type *SinDNode::Value( PhaseTransform *phase ) const {
1255  const Type *t1 = phase->type( in(1) );
1256  if( t1 == Type::TOP ) return Type::TOP;
1257  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1258  double d = t1->getd();
1259  if( d < 0.0 ) return Type::DOUBLE;
1260  return TypeD::make( SharedRuntime::dsin( d ) );
1261}
1262
1263//=============================================================================
1264//------------------------------Value------------------------------------------
1265// Compute tan
1266const Type *TanDNode::Value( PhaseTransform *phase ) const {
1267  const Type *t1 = phase->type( in(1) );
1268  if( t1 == Type::TOP ) return Type::TOP;
1269  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1270  double d = t1->getd();
1271  if( d < 0.0 ) return Type::DOUBLE;
1272  return TypeD::make( SharedRuntime::dtan( d ) );
1273}
1274
1275//=============================================================================
1276//------------------------------Value------------------------------------------
1277// Compute log
1278const Type *LogDNode::Value( PhaseTransform *phase ) const {
1279  const Type *t1 = phase->type( in(1) );
1280  if( t1 == Type::TOP ) return Type::TOP;
1281  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1282  double d = t1->getd();
1283  if( d < 0.0 ) return Type::DOUBLE;
1284  return TypeD::make( SharedRuntime::dlog( d ) );
1285}
1286
1287//=============================================================================
1288//------------------------------Value------------------------------------------
1289// Compute log10
1290const Type *Log10DNode::Value( PhaseTransform *phase ) const {
1291  const Type *t1 = phase->type( in(1) );
1292  if( t1 == Type::TOP ) return Type::TOP;
1293  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1294  double d = t1->getd();
1295  if( d < 0.0 ) return Type::DOUBLE;
1296  return TypeD::make( SharedRuntime::dlog10( d ) );
1297}
1298
1299//=============================================================================
1300//------------------------------Value------------------------------------------
1301// Compute exp
1302const Type *ExpDNode::Value( PhaseTransform *phase ) const {
1303  const Type *t1 = phase->type( in(1) );
1304  if( t1 == Type::TOP ) return Type::TOP;
1305  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1306  double d = t1->getd();
1307  if( d < 0.0 ) return Type::DOUBLE;
1308  return TypeD::make( SharedRuntime::dexp( d ) );
1309}
1310
1311
1312//=============================================================================
1313//------------------------------Value------------------------------------------
1314// Compute pow
1315const Type *PowDNode::Value( PhaseTransform *phase ) const {
1316  const Type *t1 = phase->type( in(1) );
1317  if( t1 == Type::TOP ) return Type::TOP;
1318  if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
1319  const Type *t2 = phase->type( in(2) );
1320  if( t2 == Type::TOP ) return Type::TOP;
1321  if( t2->base() != Type::DoubleCon ) return Type::DOUBLE;
1322  double d1 = t1->getd();
1323  double d2 = t2->getd();
1324  if( d1 < 0.0 ) return Type::DOUBLE;
1325  if( d2 < 0.0 ) return Type::DOUBLE;
1326  return TypeD::make( SharedRuntime::dpow( d1, d2 ) );
1327}
1328