src/share/vm/opto/subnode.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 8034812 Sdiff src/share/vm/opto

src/share/vm/opto/subnode.cpp

Print this page




 132   Node *in1 = in(1);
 133   Node *in2 = in(2);
 134   uint op1 = in1->Opcode();
 135   uint op2 = in2->Opcode();
 136 
 137 #ifdef ASSERT
 138   // Check for dead loop
 139   if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
 140       ( op1 == Op_AddI || op1 == Op_SubI ) &&
 141       ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
 142         phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1 ) ) )
 143     assert(false, "dead loop in SubINode::Ideal");
 144 #endif
 145 
 146   const Type *t2 = phase->type( in2 );
 147   if( t2 == Type::TOP ) return NULL;
 148   // Convert "x-c0" into "x+ -c0".
 149   if( t2->base() == Type::Int ){        // Might be bottom or top...
 150     const TypeInt *i = t2->is_int();
 151     if( i->is_con() )
 152       return new (phase->C) AddINode(in1, phase->intcon(-i->get_con()));
 153   }
 154 
 155   // Convert "(x+c0) - y" into (x-y) + c0"
 156   // Do not collapse (x+c0)-y if "+" is a loop increment or
 157   // if "y" is a loop induction variable.
 158   if( op1 == Op_AddI && ok_to_convert(in1, in2) ) {
 159     const Type *tadd = phase->type( in1->in(2) );
 160     if( tadd->singleton() && tadd != Type::TOP ) {
 161       Node *sub2 = phase->transform( new (phase->C) SubINode( in1->in(1), in2 ));
 162       return new (phase->C) AddINode( sub2, in1->in(2) );
 163     }
 164   }
 165 
 166 
 167   // Convert "x - (y+c0)" into "(x-y) - c0"
 168   // Need the same check as in above optimization but reversed.
 169   if (op2 == Op_AddI && ok_to_convert(in2, in1)) {
 170     Node* in21 = in2->in(1);
 171     Node* in22 = in2->in(2);
 172     const TypeInt* tcon = phase->type(in22)->isa_int();
 173     if (tcon != NULL && tcon->is_con()) {
 174       Node* sub2 = phase->transform( new (phase->C) SubINode(in1, in21) );
 175       Node* neg_c0 = phase->intcon(- tcon->get_con());
 176       return new (phase->C) AddINode(sub2, neg_c0);
 177     }
 178   }
 179 
 180   const Type *t1 = phase->type( in1 );
 181   if( t1 == Type::TOP ) return NULL;
 182 
 183 #ifdef ASSERT
 184   // Check for dead loop
 185   if( ( op2 == Op_AddI || op2 == Op_SubI ) &&
 186       ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
 187         phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
 188     assert(false, "dead loop in SubINode::Ideal");
 189 #endif
 190 
 191   // Convert "x - (x+y)" into "-y"
 192   if( op2 == Op_AddI &&
 193       phase->eqv( in1, in2->in(1) ) )
 194     return new (phase->C) SubINode( phase->intcon(0),in2->in(2));
 195   // Convert "(x-y) - x" into "-y"
 196   if( op1 == Op_SubI &&
 197       phase->eqv( in1->in(1), in2 ) )
 198     return new (phase->C) SubINode( phase->intcon(0),in1->in(2));
 199   // Convert "x - (y+x)" into "-y"
 200   if( op2 == Op_AddI &&
 201       phase->eqv( in1, in2->in(2) ) )
 202     return new (phase->C) SubINode( phase->intcon(0),in2->in(1));
 203 
 204   // Convert "0 - (x-y)" into "y-x"
 205   if( t1 == TypeInt::ZERO && op2 == Op_SubI )
 206     return new (phase->C) SubINode( in2->in(2), in2->in(1) );
 207 
 208   // Convert "0 - (x+con)" into "-con-x"
 209   jint con;
 210   if( t1 == TypeInt::ZERO && op2 == Op_AddI &&
 211       (con = in2->in(2)->find_int_con(0)) != 0 )
 212     return new (phase->C) SubINode( phase->intcon(-con), in2->in(1) );
 213 
 214   // Convert "(X+A) - (X+B)" into "A - B"
 215   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) )
 216     return new (phase->C) SubINode( in1->in(2), in2->in(2) );
 217 
 218   // Convert "(A+X) - (B+X)" into "A - B"
 219   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) )
 220     return new (phase->C) SubINode( in1->in(1), in2->in(1) );
 221 
 222   // Convert "(A+X) - (X+B)" into "A - B"
 223   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(1) )
 224     return new (phase->C) SubINode( in1->in(1), in2->in(2) );
 225 
 226   // Convert "(X+A) - (B+X)" into "A - B"
 227   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(2) )
 228     return new (phase->C) SubINode( in1->in(2), in2->in(1) );
 229 
 230   // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally
 231   // nicer to optimize than subtract.
 232   if( op2 == Op_SubI && in2->outcnt() == 1) {
 233     Node *add1 = phase->transform( new (phase->C) AddINode( in1, in2->in(2) ) );
 234     return new (phase->C) SubINode( add1, in2->in(1) );
 235   }
 236 
 237   return NULL;
 238 }
 239 
 240 //------------------------------sub--------------------------------------------
 241 // A subtract node differences it's two inputs.
 242 const Type *SubINode::sub( const Type *t1, const Type *t2 ) const {
 243   const TypeInt *r0 = t1->is_int(); // Handy access
 244   const TypeInt *r1 = t2->is_int();
 245   int32 lo = r0->_lo - r1->_hi;
 246   int32 hi = r0->_hi - r1->_lo;
 247 
 248   // We next check for 32-bit overflow.
 249   // If that happens, we just assume all integers are possible.
 250   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
 251        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
 252       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
 253        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
 254     return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen));


 261 Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 262   Node *in1 = in(1);
 263   Node *in2 = in(2);
 264   uint op1 = in1->Opcode();
 265   uint op2 = in2->Opcode();
 266 
 267 #ifdef ASSERT
 268   // Check for dead loop
 269   if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
 270       ( op1 == Op_AddL || op1 == Op_SubL ) &&
 271       ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
 272         phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1  ) ) )
 273     assert(false, "dead loop in SubLNode::Ideal");
 274 #endif
 275 
 276   if( phase->type( in2 ) == Type::TOP ) return NULL;
 277   const TypeLong *i = phase->type( in2 )->isa_long();
 278   // Convert "x-c0" into "x+ -c0".
 279   if( i &&                      // Might be bottom or top...
 280       i->is_con() )
 281     return new (phase->C) AddLNode(in1, phase->longcon(-i->get_con()));
 282 
 283   // Convert "(x+c0) - y" into (x-y) + c0"
 284   // Do not collapse (x+c0)-y if "+" is a loop increment or
 285   // if "y" is a loop induction variable.
 286   if( op1 == Op_AddL && ok_to_convert(in1, in2) ) {
 287     Node *in11 = in1->in(1);
 288     const Type *tadd = phase->type( in1->in(2) );
 289     if( tadd->singleton() && tadd != Type::TOP ) {
 290       Node *sub2 = phase->transform( new (phase->C) SubLNode( in11, in2 ));
 291       return new (phase->C) AddLNode( sub2, in1->in(2) );
 292     }
 293   }
 294 
 295   // Convert "x - (y+c0)" into "(x-y) - c0"
 296   // Need the same check as in above optimization but reversed.
 297   if (op2 == Op_AddL && ok_to_convert(in2, in1)) {
 298     Node* in21 = in2->in(1);
 299     Node* in22 = in2->in(2);
 300     const TypeLong* tcon = phase->type(in22)->isa_long();
 301     if (tcon != NULL && tcon->is_con()) {
 302       Node* sub2 = phase->transform( new (phase->C) SubLNode(in1, in21) );
 303       Node* neg_c0 = phase->longcon(- tcon->get_con());
 304       return new (phase->C) AddLNode(sub2, neg_c0);
 305     }
 306   }
 307 
 308   const Type *t1 = phase->type( in1 );
 309   if( t1 == Type::TOP ) return NULL;
 310 
 311 #ifdef ASSERT
 312   // Check for dead loop
 313   if( ( op2 == Op_AddL || op2 == Op_SubL ) &&
 314       ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
 315         phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
 316     assert(false, "dead loop in SubLNode::Ideal");
 317 #endif
 318 
 319   // Convert "x - (x+y)" into "-y"
 320   if( op2 == Op_AddL &&
 321       phase->eqv( in1, in2->in(1) ) )
 322     return new (phase->C) SubLNode( phase->makecon(TypeLong::ZERO), in2->in(2));
 323   // Convert "x - (y+x)" into "-y"
 324   if( op2 == Op_AddL &&
 325       phase->eqv( in1, in2->in(2) ) )
 326     return new (phase->C) SubLNode( phase->makecon(TypeLong::ZERO),in2->in(1));
 327 
 328   // Convert "0 - (x-y)" into "y-x"
 329   if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL )
 330     return new (phase->C) SubLNode( in2->in(2), in2->in(1) );
 331 
 332   // Convert "(X+A) - (X+B)" into "A - B"
 333   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) )
 334     return new (phase->C) SubLNode( in1->in(2), in2->in(2) );
 335 
 336   // Convert "(A+X) - (B+X)" into "A - B"
 337   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) )
 338     return new (phase->C) SubLNode( in1->in(1), in2->in(1) );
 339 
 340   // Convert "A-(B-C)" into (A+C)-B"
 341   if( op2 == Op_SubL && in2->outcnt() == 1) {
 342     Node *add1 = phase->transform( new (phase->C) AddLNode( in1, in2->in(2) ) );
 343     return new (phase->C) SubLNode( add1, in2->in(1) );
 344   }
 345 
 346   return NULL;
 347 }
 348 
 349 //------------------------------sub--------------------------------------------
 350 // A subtract node differences it's two inputs.
 351 const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const {
 352   const TypeLong *r0 = t1->is_long(); // Handy access
 353   const TypeLong *r1 = t2->is_long();
 354   jlong lo = r0->_lo - r1->_hi;
 355   jlong hi = r0->_hi - r1->_lo;
 356 
 357   // We next check for 32-bit overflow.
 358   // If that happens, we just assume all integers are possible.
 359   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
 360        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
 361       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
 362        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
 363     return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen));


 390     return bot;
 391 
 392   return sub(t1,t2);            // Local flavor of type subtraction
 393 }
 394 
 395 
 396 //=============================================================================
 397 //------------------------------Ideal------------------------------------------
 398 Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 399   const Type *t2 = phase->type( in(2) );
 400   // Convert "x-c0" into "x+ -c0".
 401   if( t2->base() == Type::FloatCon ) {  // Might be bottom or top...
 402     // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) );
 403   }
 404 
 405   // Not associative because of boundary conditions (infinity)
 406   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
 407     // Convert "x - (x+y)" into "-y"
 408     if( in(2)->is_Add() &&
 409         phase->eqv(in(1),in(2)->in(1) ) )
 410       return new (phase->C) SubFNode( phase->makecon(TypeF::ZERO),in(2)->in(2));
 411   }
 412 
 413   // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes
 414   // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0.
 415   //if( phase->type(in(1)) == TypeF::ZERO )
 416   //return new (phase->C, 2) NegFNode(in(2));
 417 
 418   return NULL;
 419 }
 420 
 421 //------------------------------sub--------------------------------------------
 422 // A subtract node differences its two inputs.
 423 const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const {
 424   // no folding if one of operands is infinity or NaN, do not do constant folding
 425   if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) {
 426     return TypeF::make( t1->getf() - t2->getf() );
 427   }
 428   else if( g_isnan(t1->getf()) ) {
 429     return t1;
 430   }


 433   }
 434   else {
 435     return Type::FLOAT;
 436   }
 437 }
 438 
 439 //=============================================================================
 440 //------------------------------Ideal------------------------------------------
 441 Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){
 442   const Type *t2 = phase->type( in(2) );
 443   // Convert "x-c0" into "x+ -c0".
 444   if( t2->base() == Type::DoubleCon ) { // Might be bottom or top...
 445     // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) );
 446   }
 447 
 448   // Not associative because of boundary conditions (infinity)
 449   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
 450     // Convert "x - (x+y)" into "-y"
 451     if( in(2)->is_Add() &&
 452         phase->eqv(in(1),in(2)->in(1) ) )
 453       return new (phase->C) SubDNode( phase->makecon(TypeD::ZERO),in(2)->in(2));
 454   }
 455 
 456   // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes
 457   // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0.
 458   //if( phase->type(in(1)) == TypeD::ZERO )
 459   //return new (phase->C, 2) NegDNode(in(2));
 460 
 461   return NULL;
 462 }
 463 
 464 //------------------------------sub--------------------------------------------
 465 // A subtract node differences its two inputs.
 466 const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const {
 467   // no folding if one of operands is infinity or NaN, do not do constant folding
 468   if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) {
 469     return TypeD::make( t1->getd() - t2->getd() );
 470   }
 471   else if( g_isnan(t1->getd()) ) {
 472     return t1;
 473   }


 564   // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have
 565   // to be positive.
 566   // (This is a gross hack, since the sub method never
 567   // looks at the structure of the node in any other case.)
 568   if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check())
 569     return TypeInt::CC_LT;
 570   return TypeInt::CC;                   // else use worst case results
 571 }
 572 
 573 bool CmpUNode::is_index_range_check() const {
 574   // Check for the "(X ModI Y) CmpU Y" shape
 575   return (in(1)->Opcode() == Op_ModI &&
 576           in(1)->in(2)->eqv_uncast(in(2)));
 577 }
 578 
 579 //------------------------------Idealize---------------------------------------
 580 Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
 581   if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
 582     switch (in(1)->Opcode()) {
 583     case Op_CmpL3:              // Collapse a CmpL3/CmpI into a CmpL
 584       return new (phase->C) CmpLNode(in(1)->in(1),in(1)->in(2));
 585     case Op_CmpF3:              // Collapse a CmpF3/CmpI into a CmpF
 586       return new (phase->C) CmpFNode(in(1)->in(1),in(1)->in(2));
 587     case Op_CmpD3:              // Collapse a CmpD3/CmpI into a CmpD
 588       return new (phase->C) CmpDNode(in(1)->in(1),in(1)->in(2));
 589     //case Op_SubI:
 590       // If (x - y) cannot overflow, then ((x - y) <?> 0)
 591       // can be turned into (x <?> y).
 592       // This is handled (with more general cases) by Ideal_sub_algebra.
 593     }
 594   }
 595   return NULL;                  // No change
 596 }
 597 
 598 
 599 //=============================================================================
 600 // Simplify a CmpL (compare 2 longs ) node, based on local information.
 601 // If both inputs are constants, compare them.
 602 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
 603   const TypeLong *r0 = t1->is_long(); // Handy access
 604   const TypeLong *r1 = t2->is_long();
 605 
 606   if( r0->_hi < r1->_lo )       // Range is always low?
 607     return TypeInt::CC_LT;
 608   else if( r0->_lo > r1->_hi )  // Range is always high?


1007     idx_f2d = 2;                // No, swap to check for reversed args
1008   int idx_con = 3-idx_f2d;      // Check for the constant on other input
1009 
1010   if( ConvertCmpD2CmpF &&
1011       in(idx_f2d)->Opcode() == Op_ConvF2D &&
1012       in(idx_con)->Opcode() == Op_ConD ) {
1013     const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
1014     double t2_value_as_double = t2->_d;
1015     float  t2_value_as_float  = (float)t2_value_as_double;
1016     if( t2_value_as_double == (double)t2_value_as_float ) {
1017       // Test value can be represented as a float
1018       // Eliminate the conversion to double and create new comparison
1019       Node *new_in1 = in(idx_f2d)->in(1);
1020       Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
1021       if( idx_f2d != 1 ) {      // Must flip args to match original order
1022         Node *tmp = new_in1;
1023         new_in1 = new_in2;
1024         new_in2 = tmp;
1025       }
1026       CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
1027         ? new (phase->C) CmpF3Node( new_in1, new_in2 )
1028         : new (phase->C) CmpFNode ( new_in1, new_in2 ) ;
1029       return new_cmp;           // Changed to CmpFNode
1030     }
1031     // Testing value required the precision of a double
1032   }
1033   return NULL;                  // No change
1034 }
1035 
1036 
1037 //=============================================================================
1038 //------------------------------cc2logical-------------------------------------
1039 // Convert a condition code type to a logical type
1040 const Type *BoolTest::cc2logical( const Type *CC ) const {
1041   if( CC == Type::TOP ) return Type::TOP;
1042   if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
1043   const TypeInt *ti = CC->is_int();
1044   if( ti->is_con() ) {          // Only 1 kind of condition codes set?
1045     // Match low order 2 bits
1046     int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
1047     if( _test & 4 ) tmp = 1-tmp;     // Optionally complement result
1048     return TypeInt::make(tmp);       // Boolean result


1080 }
1081 
1082 //-------------------------------make_predicate--------------------------------
1083 Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
1084   if (test_value->is_Con())   return test_value;
1085   if (test_value->is_Bool())  return test_value;
1086   Compile* C = phase->C;
1087   if (test_value->is_CMove() &&
1088       test_value->in(CMoveNode::Condition)->is_Bool()) {
1089     BoolNode*   bol   = test_value->in(CMoveNode::Condition)->as_Bool();
1090     const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
1091     const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
1092     if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
1093       return bol;
1094     } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
1095       return phase->transform( bol->negate(phase) );
1096     }
1097     // Else fall through.  The CMove gets in the way of the test.
1098     // It should be the case that make_predicate(bol->as_int_value()) == bol.
1099   }
1100   Node* cmp = new (C) CmpINode(test_value, phase->intcon(0));
1101   cmp = phase->transform(cmp);
1102   Node* bol = new (C) BoolNode(cmp, BoolTest::ne);
1103   return phase->transform(bol);
1104 }
1105 
1106 //--------------------------------as_int_value---------------------------------
1107 Node* BoolNode::as_int_value(PhaseGVN* phase) {
1108   // Inverse to make_predicate.  The CMove probably boils down to a Conv2B.
1109   Node* cmov = CMoveNode::make(phase->C, NULL, this,
1110                                phase->intcon(0), phase->intcon(1),
1111                                TypeInt::BOOL);
1112   return phase->transform(cmov);
1113 }
1114 
1115 //----------------------------------negate-------------------------------------
1116 BoolNode* BoolNode::negate(PhaseGVN* phase) {
1117   Compile* C = phase->C;
1118   return new (C) BoolNode(in(1), _test.negate());
1119 }
1120 
1121 
1122 //------------------------------Ideal------------------------------------------
1123 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1124   // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
1125   // This moves the constant to the right.  Helps value-numbering.
1126   Node *cmp = in(1);
1127   if( !cmp->is_Sub() ) return NULL;
1128   int cop = cmp->Opcode();
1129   if( cop == Op_FastLock || cop == Op_FastUnlock) return NULL;
1130   Node *cmp1 = cmp->in(1);
1131   Node *cmp2 = cmp->in(2);
1132   if( !cmp1 ) return NULL;
1133 
1134   if (_test._test == BoolTest::overflow || _test._test == BoolTest::no_overflow) {
1135     return NULL;
1136   }
1137 
1138   // Constant on left?
1139   Node *con = cmp1;
1140   uint op2 = cmp2->Opcode();
1141   // Move constants to the right of compare's to canonicalize.
1142   // Do not muck with Opaque1 nodes, as this indicates a loop
1143   // guard that cannot change shape.
1144   if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 &&
1145       // Because of NaN's, CmpD and CmpF are not commutative
1146       cop != Op_CmpD && cop != Op_CmpF &&
1147       // Protect against swapping inputs to a compare when it is used by a
1148       // counted loop exit, which requires maintaining the loop-limit as in(2)
1149       !is_counted_loop_exit_test() ) {
1150     // Ok, commute the constant to the right of the cmp node.
1151     // Clone the Node, getting a new Node of the same class
1152     cmp = cmp->clone();
1153     // Swap inputs to the clone
1154     cmp->swap_edges(1, 2);
1155     cmp = phase->transform( cmp );
1156     return new (phase->C) BoolNode( cmp, _test.commute() );
1157   }
1158 
1159   // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
1160   // The XOR-1 is an idiom used to flip the sense of a bool.  We flip the
1161   // test instead.
1162   int cmp1_op = cmp1->Opcode();
1163   const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
1164   if (cmp2_type == NULL)  return NULL;
1165   Node* j_xor = cmp1;
1166   if( cmp2_type == TypeInt::ZERO &&
1167       cmp1_op == Op_XorI &&
1168       j_xor->in(1) != j_xor &&          // An xor of itself is dead
1169       phase->type( j_xor->in(1) ) == TypeInt::BOOL &&
1170       phase->type( j_xor->in(2) ) == TypeInt::ONE &&
1171       (_test._test == BoolTest::eq ||
1172        _test._test == BoolTest::ne) ) {
1173     Node *ncmp = phase->transform(new (phase->C) CmpINode(j_xor->in(1),cmp2));
1174     return new (phase->C) BoolNode( ncmp, _test.negate() );
1175   }
1176 
1177   // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
1178   // This is a standard idiom for branching on a boolean value.
1179   Node *c2b = cmp1;
1180   if( cmp2_type == TypeInt::ZERO &&
1181       cmp1_op == Op_Conv2B &&
1182       (_test._test == BoolTest::eq ||
1183        _test._test == BoolTest::ne) ) {
1184     Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
1185        ? (Node*)new (phase->C) CmpINode(c2b->in(1),cmp2)
1186        : (Node*)new (phase->C) CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
1187     );
1188     return new (phase->C) BoolNode( ncmp, _test._test );
1189   }
1190 
1191   // Comparing a SubI against a zero is equal to comparing the SubI
1192   // arguments directly.  This only works for eq and ne comparisons
1193   // due to possible integer overflow.
1194   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1195         (cop == Op_CmpI) &&
1196         (cmp1->Opcode() == Op_SubI) &&
1197         ( cmp2_type == TypeInt::ZERO ) ) {
1198     Node *ncmp = phase->transform( new (phase->C) CmpINode(cmp1->in(1),cmp1->in(2)));
1199     return new (phase->C) BoolNode( ncmp, _test._test );
1200   }
1201 
1202   // Change (-A vs 0) into (A vs 0) by commuting the test.  Disallow in the
1203   // most general case because negating 0x80000000 does nothing.  Needed for
1204   // the CmpF3/SubI/CmpI idiom.
1205   if( cop == Op_CmpI &&
1206       cmp1->Opcode() == Op_SubI &&
1207       cmp2_type == TypeInt::ZERO &&
1208       phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
1209       phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
1210     Node *ncmp = phase->transform( new (phase->C) CmpINode(cmp1->in(2),cmp2));
1211     return new (phase->C) BoolNode( ncmp, _test.commute() );
1212   }
1213 
1214   //  The transformation below is not valid for either signed or unsigned
1215   //  comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
1216   //  This transformation can be resurrected when we are able to
1217   //  make inferences about the range of values being subtracted from
1218   //  (or added to) relative to the wraparound point.
1219   //
1220   //    // Remove +/-1's if possible.
1221   //    // "X <= Y-1" becomes "X <  Y"
1222   //    // "X+1 <= Y" becomes "X <  Y"
1223   //    // "X <  Y+1" becomes "X <= Y"
1224   //    // "X-1 <  Y" becomes "X <= Y"
1225   //    // Do not this to compares off of the counted-loop-end.  These guys are
1226   //    // checking the trip counter and they want to use the post-incremented
1227   //    // counter.  If they use the PRE-incremented counter, then the counter has
1228   //    // to be incremented in a private block on a loop backedge.
1229   //    if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
1230   //      return NULL;
1231   //  #ifndef PRODUCT




 132   Node *in1 = in(1);
 133   Node *in2 = in(2);
 134   uint op1 = in1->Opcode();
 135   uint op2 = in2->Opcode();
 136 
 137 #ifdef ASSERT
 138   // Check for dead loop
 139   if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
 140       ( op1 == Op_AddI || op1 == Op_SubI ) &&
 141       ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
 142         phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1 ) ) )
 143     assert(false, "dead loop in SubINode::Ideal");
 144 #endif
 145 
 146   const Type *t2 = phase->type( in2 );
 147   if( t2 == Type::TOP ) return NULL;
 148   // Convert "x-c0" into "x+ -c0".
 149   if( t2->base() == Type::Int ){        // Might be bottom or top...
 150     const TypeInt *i = t2->is_int();
 151     if( i->is_con() )
 152       return new AddINode(in1, phase->intcon(-i->get_con()));
 153   }
 154 
 155   // Convert "(x+c0) - y" into (x-y) + c0"
 156   // Do not collapse (x+c0)-y if "+" is a loop increment or
 157   // if "y" is a loop induction variable.
 158   if( op1 == Op_AddI && ok_to_convert(in1, in2) ) {
 159     const Type *tadd = phase->type( in1->in(2) );
 160     if( tadd->singleton() && tadd != Type::TOP ) {
 161       Node *sub2 = phase->transform( new SubINode( in1->in(1), in2 ));
 162       return new AddINode( sub2, in1->in(2) );
 163     }
 164   }
 165 
 166 
 167   // Convert "x - (y+c0)" into "(x-y) - c0"
 168   // Need the same check as in above optimization but reversed.
 169   if (op2 == Op_AddI && ok_to_convert(in2, in1)) {
 170     Node* in21 = in2->in(1);
 171     Node* in22 = in2->in(2);
 172     const TypeInt* tcon = phase->type(in22)->isa_int();
 173     if (tcon != NULL && tcon->is_con()) {
 174       Node* sub2 = phase->transform( new SubINode(in1, in21) );
 175       Node* neg_c0 = phase->intcon(- tcon->get_con());
 176       return new AddINode(sub2, neg_c0);
 177     }
 178   }
 179 
 180   const Type *t1 = phase->type( in1 );
 181   if( t1 == Type::TOP ) return NULL;
 182 
 183 #ifdef ASSERT
 184   // Check for dead loop
 185   if( ( op2 == Op_AddI || op2 == Op_SubI ) &&
 186       ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
 187         phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
 188     assert(false, "dead loop in SubINode::Ideal");
 189 #endif
 190 
 191   // Convert "x - (x+y)" into "-y"
 192   if( op2 == Op_AddI &&
 193       phase->eqv( in1, in2->in(1) ) )
 194     return new SubINode( phase->intcon(0),in2->in(2));
 195   // Convert "(x-y) - x" into "-y"
 196   if( op1 == Op_SubI &&
 197       phase->eqv( in1->in(1), in2 ) )
 198     return new SubINode( phase->intcon(0),in1->in(2));
 199   // Convert "x - (y+x)" into "-y"
 200   if( op2 == Op_AddI &&
 201       phase->eqv( in1, in2->in(2) ) )
 202     return new SubINode( phase->intcon(0),in2->in(1));
 203 
 204   // Convert "0 - (x-y)" into "y-x"
 205   if( t1 == TypeInt::ZERO && op2 == Op_SubI )
 206     return new SubINode( in2->in(2), in2->in(1) );
 207 
 208   // Convert "0 - (x+con)" into "-con-x"
 209   jint con;
 210   if( t1 == TypeInt::ZERO && op2 == Op_AddI &&
 211       (con = in2->in(2)->find_int_con(0)) != 0 )
 212     return new SubINode( phase->intcon(-con), in2->in(1) );
 213 
 214   // Convert "(X+A) - (X+B)" into "A - B"
 215   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) )
 216     return new SubINode( in1->in(2), in2->in(2) );
 217 
 218   // Convert "(A+X) - (B+X)" into "A - B"
 219   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) )
 220     return new SubINode( in1->in(1), in2->in(1) );
 221 
 222   // Convert "(A+X) - (X+B)" into "A - B"
 223   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(1) )
 224     return new SubINode( in1->in(1), in2->in(2) );
 225 
 226   // Convert "(X+A) - (B+X)" into "A - B"
 227   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(2) )
 228     return new SubINode( in1->in(2), in2->in(1) );
 229 
 230   // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally
 231   // nicer to optimize than subtract.
 232   if( op2 == Op_SubI && in2->outcnt() == 1) {
 233     Node *add1 = phase->transform( new AddINode( in1, in2->in(2) ) );
 234     return new SubINode( add1, in2->in(1) );
 235   }
 236 
 237   return NULL;
 238 }
 239 
 240 //------------------------------sub--------------------------------------------
 241 // A subtract node differences it's two inputs.
 242 const Type *SubINode::sub( const Type *t1, const Type *t2 ) const {
 243   const TypeInt *r0 = t1->is_int(); // Handy access
 244   const TypeInt *r1 = t2->is_int();
 245   int32 lo = r0->_lo - r1->_hi;
 246   int32 hi = r0->_hi - r1->_lo;
 247 
 248   // We next check for 32-bit overflow.
 249   // If that happens, we just assume all integers are possible.
 250   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
 251        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
 252       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
 253        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
 254     return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen));


 261 Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 262   Node *in1 = in(1);
 263   Node *in2 = in(2);
 264   uint op1 = in1->Opcode();
 265   uint op2 = in2->Opcode();
 266 
 267 #ifdef ASSERT
 268   // Check for dead loop
 269   if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
 270       ( op1 == Op_AddL || op1 == Op_SubL ) &&
 271       ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
 272         phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1  ) ) )
 273     assert(false, "dead loop in SubLNode::Ideal");
 274 #endif
 275 
 276   if( phase->type( in2 ) == Type::TOP ) return NULL;
 277   const TypeLong *i = phase->type( in2 )->isa_long();
 278   // Convert "x-c0" into "x+ -c0".
 279   if( i &&                      // Might be bottom or top...
 280       i->is_con() )
 281     return new AddLNode(in1, phase->longcon(-i->get_con()));
 282 
 283   // Convert "(x+c0) - y" into (x-y) + c0"
 284   // Do not collapse (x+c0)-y if "+" is a loop increment or
 285   // if "y" is a loop induction variable.
 286   if( op1 == Op_AddL && ok_to_convert(in1, in2) ) {
 287     Node *in11 = in1->in(1);
 288     const Type *tadd = phase->type( in1->in(2) );
 289     if( tadd->singleton() && tadd != Type::TOP ) {
 290       Node *sub2 = phase->transform( new SubLNode( in11, in2 ));
 291       return new AddLNode( sub2, in1->in(2) );
 292     }
 293   }
 294 
 295   // Convert "x - (y+c0)" into "(x-y) - c0"
 296   // Need the same check as in above optimization but reversed.
 297   if (op2 == Op_AddL && ok_to_convert(in2, in1)) {
 298     Node* in21 = in2->in(1);
 299     Node* in22 = in2->in(2);
 300     const TypeLong* tcon = phase->type(in22)->isa_long();
 301     if (tcon != NULL && tcon->is_con()) {
 302       Node* sub2 = phase->transform( new SubLNode(in1, in21) );
 303       Node* neg_c0 = phase->longcon(- tcon->get_con());
 304       return new AddLNode(sub2, neg_c0);
 305     }
 306   }
 307 
 308   const Type *t1 = phase->type( in1 );
 309   if( t1 == Type::TOP ) return NULL;
 310 
 311 #ifdef ASSERT
 312   // Check for dead loop
 313   if( ( op2 == Op_AddL || op2 == Op_SubL ) &&
 314       ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
 315         phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
 316     assert(false, "dead loop in SubLNode::Ideal");
 317 #endif
 318 
 319   // Convert "x - (x+y)" into "-y"
 320   if( op2 == Op_AddL &&
 321       phase->eqv( in1, in2->in(1) ) )
 322     return new SubLNode( phase->makecon(TypeLong::ZERO), in2->in(2));
 323   // Convert "x - (y+x)" into "-y"
 324   if( op2 == Op_AddL &&
 325       phase->eqv( in1, in2->in(2) ) )
 326     return new SubLNode( phase->makecon(TypeLong::ZERO),in2->in(1));
 327 
 328   // Convert "0 - (x-y)" into "y-x"
 329   if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL )
 330     return new SubLNode( in2->in(2), in2->in(1) );
 331 
 332   // Convert "(X+A) - (X+B)" into "A - B"
 333   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) )
 334     return new SubLNode( in1->in(2), in2->in(2) );
 335 
 336   // Convert "(A+X) - (B+X)" into "A - B"
 337   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) )
 338     return new SubLNode( in1->in(1), in2->in(1) );
 339 
 340   // Convert "A-(B-C)" into (A+C)-B"
 341   if( op2 == Op_SubL && in2->outcnt() == 1) {
 342     Node *add1 = phase->transform( new AddLNode( in1, in2->in(2) ) );
 343     return new SubLNode( add1, in2->in(1) );
 344   }
 345 
 346   return NULL;
 347 }
 348 
 349 //------------------------------sub--------------------------------------------
 350 // A subtract node differences it's two inputs.
 351 const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const {
 352   const TypeLong *r0 = t1->is_long(); // Handy access
 353   const TypeLong *r1 = t2->is_long();
 354   jlong lo = r0->_lo - r1->_hi;
 355   jlong hi = r0->_hi - r1->_lo;
 356 
 357   // We next check for 32-bit overflow.
 358   // If that happens, we just assume all integers are possible.
 359   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
 360        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
 361       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
 362        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
 363     return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen));


 390     return bot;
 391 
 392   return sub(t1,t2);            // Local flavor of type subtraction
 393 }
 394 
 395 
 396 //=============================================================================
 397 //------------------------------Ideal------------------------------------------
 398 Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 399   const Type *t2 = phase->type( in(2) );
 400   // Convert "x-c0" into "x+ -c0".
 401   if( t2->base() == Type::FloatCon ) {  // Might be bottom or top...
 402     // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) );
 403   }
 404 
 405   // Not associative because of boundary conditions (infinity)
 406   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
 407     // Convert "x - (x+y)" into "-y"
 408     if( in(2)->is_Add() &&
 409         phase->eqv(in(1),in(2)->in(1) ) )
 410       return new SubFNode( phase->makecon(TypeF::ZERO),in(2)->in(2));
 411   }
 412 
 413   // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes
 414   // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0.
 415   //if( phase->type(in(1)) == TypeF::ZERO )
 416   //return new (phase->C, 2) NegFNode(in(2));
 417 
 418   return NULL;
 419 }
 420 
 421 //------------------------------sub--------------------------------------------
 422 // A subtract node differences its two inputs.
 423 const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const {
 424   // no folding if one of operands is infinity or NaN, do not do constant folding
 425   if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) {
 426     return TypeF::make( t1->getf() - t2->getf() );
 427   }
 428   else if( g_isnan(t1->getf()) ) {
 429     return t1;
 430   }


 433   }
 434   else {
 435     return Type::FLOAT;
 436   }
 437 }
 438 
 439 //=============================================================================
 440 //------------------------------Ideal------------------------------------------
 441 Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){
 442   const Type *t2 = phase->type( in(2) );
 443   // Convert "x-c0" into "x+ -c0".
 444   if( t2->base() == Type::DoubleCon ) { // Might be bottom or top...
 445     // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) );
 446   }
 447 
 448   // Not associative because of boundary conditions (infinity)
 449   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
 450     // Convert "x - (x+y)" into "-y"
 451     if( in(2)->is_Add() &&
 452         phase->eqv(in(1),in(2)->in(1) ) )
 453       return new SubDNode( phase->makecon(TypeD::ZERO),in(2)->in(2));
 454   }
 455 
 456   // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes
 457   // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0.
 458   //if( phase->type(in(1)) == TypeD::ZERO )
 459   //return new (phase->C, 2) NegDNode(in(2));
 460 
 461   return NULL;
 462 }
 463 
 464 //------------------------------sub--------------------------------------------
 465 // A subtract node differences its two inputs.
 466 const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const {
 467   // no folding if one of operands is infinity or NaN, do not do constant folding
 468   if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) {
 469     return TypeD::make( t1->getd() - t2->getd() );
 470   }
 471   else if( g_isnan(t1->getd()) ) {
 472     return t1;
 473   }


 564   // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have
 565   // to be positive.
 566   // (This is a gross hack, since the sub method never
 567   // looks at the structure of the node in any other case.)
 568   if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && is_index_range_check())
 569     return TypeInt::CC_LT;
 570   return TypeInt::CC;                   // else use worst case results
 571 }
 572 
 573 bool CmpUNode::is_index_range_check() const {
 574   // Check for the "(X ModI Y) CmpU Y" shape
 575   return (in(1)->Opcode() == Op_ModI &&
 576           in(1)->in(2)->eqv_uncast(in(2)));
 577 }
 578 
 579 //------------------------------Idealize---------------------------------------
 580 Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
 581   if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
 582     switch (in(1)->Opcode()) {
 583     case Op_CmpL3:              // Collapse a CmpL3/CmpI into a CmpL
 584       return new CmpLNode(in(1)->in(1),in(1)->in(2));
 585     case Op_CmpF3:              // Collapse a CmpF3/CmpI into a CmpF
 586       return new CmpFNode(in(1)->in(1),in(1)->in(2));
 587     case Op_CmpD3:              // Collapse a CmpD3/CmpI into a CmpD
 588       return new CmpDNode(in(1)->in(1),in(1)->in(2));
 589     //case Op_SubI:
 590       // If (x - y) cannot overflow, then ((x - y) <?> 0)
 591       // can be turned into (x <?> y).
 592       // This is handled (with more general cases) by Ideal_sub_algebra.
 593     }
 594   }
 595   return NULL;                  // No change
 596 }
 597 
 598 
 599 //=============================================================================
 600 // Simplify a CmpL (compare 2 longs ) node, based on local information.
 601 // If both inputs are constants, compare them.
 602 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
 603   const TypeLong *r0 = t1->is_long(); // Handy access
 604   const TypeLong *r1 = t2->is_long();
 605 
 606   if( r0->_hi < r1->_lo )       // Range is always low?
 607     return TypeInt::CC_LT;
 608   else if( r0->_lo > r1->_hi )  // Range is always high?


1007     idx_f2d = 2;                // No, swap to check for reversed args
1008   int idx_con = 3-idx_f2d;      // Check for the constant on other input
1009 
1010   if( ConvertCmpD2CmpF &&
1011       in(idx_f2d)->Opcode() == Op_ConvF2D &&
1012       in(idx_con)->Opcode() == Op_ConD ) {
1013     const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
1014     double t2_value_as_double = t2->_d;
1015     float  t2_value_as_float  = (float)t2_value_as_double;
1016     if( t2_value_as_double == (double)t2_value_as_float ) {
1017       // Test value can be represented as a float
1018       // Eliminate the conversion to double and create new comparison
1019       Node *new_in1 = in(idx_f2d)->in(1);
1020       Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
1021       if( idx_f2d != 1 ) {      // Must flip args to match original order
1022         Node *tmp = new_in1;
1023         new_in1 = new_in2;
1024         new_in2 = tmp;
1025       }
1026       CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
1027         ? new CmpF3Node( new_in1, new_in2 )
1028         : new CmpFNode ( new_in1, new_in2 ) ;
1029       return new_cmp;           // Changed to CmpFNode
1030     }
1031     // Testing value required the precision of a double
1032   }
1033   return NULL;                  // No change
1034 }
1035 
1036 
1037 //=============================================================================
1038 //------------------------------cc2logical-------------------------------------
1039 // Convert a condition code type to a logical type
1040 const Type *BoolTest::cc2logical( const Type *CC ) const {
1041   if( CC == Type::TOP ) return Type::TOP;
1042   if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
1043   const TypeInt *ti = CC->is_int();
1044   if( ti->is_con() ) {          // Only 1 kind of condition codes set?
1045     // Match low order 2 bits
1046     int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
1047     if( _test & 4 ) tmp = 1-tmp;     // Optionally complement result
1048     return TypeInt::make(tmp);       // Boolean result


1080 }
1081 
1082 //-------------------------------make_predicate--------------------------------
1083 Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
1084   if (test_value->is_Con())   return test_value;
1085   if (test_value->is_Bool())  return test_value;
1086   Compile* C = phase->C;
1087   if (test_value->is_CMove() &&
1088       test_value->in(CMoveNode::Condition)->is_Bool()) {
1089     BoolNode*   bol   = test_value->in(CMoveNode::Condition)->as_Bool();
1090     const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
1091     const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
1092     if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
1093       return bol;
1094     } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
1095       return phase->transform( bol->negate(phase) );
1096     }
1097     // Else fall through.  The CMove gets in the way of the test.
1098     // It should be the case that make_predicate(bol->as_int_value()) == bol.
1099   }
1100   Node* cmp = new CmpINode(test_value, phase->intcon(0));
1101   cmp = phase->transform(cmp);
1102   Node* bol = new BoolNode(cmp, BoolTest::ne);
1103   return phase->transform(bol);
1104 }
1105 
1106 //--------------------------------as_int_value---------------------------------
1107 Node* BoolNode::as_int_value(PhaseGVN* phase) {
1108   // Inverse to make_predicate.  The CMove probably boils down to a Conv2B.
1109   Node* cmov = CMoveNode::make(phase->C, NULL, this,
1110                                phase->intcon(0), phase->intcon(1),
1111                                TypeInt::BOOL);
1112   return phase->transform(cmov);
1113 }
1114 
1115 //----------------------------------negate-------------------------------------
1116 BoolNode* BoolNode::negate(PhaseGVN* phase) {
1117   Compile* C = phase->C;
1118   return new BoolNode(in(1), _test.negate());
1119 }
1120 
1121 
1122 //------------------------------Ideal------------------------------------------
1123 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1124   // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
1125   // This moves the constant to the right.  Helps value-numbering.
1126   Node *cmp = in(1);
1127   if( !cmp->is_Sub() ) return NULL;
1128   int cop = cmp->Opcode();
1129   if( cop == Op_FastLock || cop == Op_FastUnlock) return NULL;
1130   Node *cmp1 = cmp->in(1);
1131   Node *cmp2 = cmp->in(2);
1132   if( !cmp1 ) return NULL;
1133 
1134   if (_test._test == BoolTest::overflow || _test._test == BoolTest::no_overflow) {
1135     return NULL;
1136   }
1137 
1138   // Constant on left?
1139   Node *con = cmp1;
1140   uint op2 = cmp2->Opcode();
1141   // Move constants to the right of compare's to canonicalize.
1142   // Do not muck with Opaque1 nodes, as this indicates a loop
1143   // guard that cannot change shape.
1144   if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 &&
1145       // Because of NaN's, CmpD and CmpF are not commutative
1146       cop != Op_CmpD && cop != Op_CmpF &&
1147       // Protect against swapping inputs to a compare when it is used by a
1148       // counted loop exit, which requires maintaining the loop-limit as in(2)
1149       !is_counted_loop_exit_test() ) {
1150     // Ok, commute the constant to the right of the cmp node.
1151     // Clone the Node, getting a new Node of the same class
1152     cmp = cmp->clone();
1153     // Swap inputs to the clone
1154     cmp->swap_edges(1, 2);
1155     cmp = phase->transform( cmp );
1156     return new BoolNode( cmp, _test.commute() );
1157   }
1158 
1159   // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
1160   // The XOR-1 is an idiom used to flip the sense of a bool.  We flip the
1161   // test instead.
1162   int cmp1_op = cmp1->Opcode();
1163   const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
1164   if (cmp2_type == NULL)  return NULL;
1165   Node* j_xor = cmp1;
1166   if( cmp2_type == TypeInt::ZERO &&
1167       cmp1_op == Op_XorI &&
1168       j_xor->in(1) != j_xor &&          // An xor of itself is dead
1169       phase->type( j_xor->in(1) ) == TypeInt::BOOL &&
1170       phase->type( j_xor->in(2) ) == TypeInt::ONE &&
1171       (_test._test == BoolTest::eq ||
1172        _test._test == BoolTest::ne) ) {
1173     Node *ncmp = phase->transform(new CmpINode(j_xor->in(1),cmp2));
1174     return new BoolNode( ncmp, _test.negate() );
1175   }
1176 
1177   // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
1178   // This is a standard idiom for branching on a boolean value.
1179   Node *c2b = cmp1;
1180   if( cmp2_type == TypeInt::ZERO &&
1181       cmp1_op == Op_Conv2B &&
1182       (_test._test == BoolTest::eq ||
1183        _test._test == BoolTest::ne) ) {
1184     Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
1185        ? (Node*)new CmpINode(c2b->in(1),cmp2)
1186        : (Node*)new CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
1187     );
1188     return new BoolNode( ncmp, _test._test );
1189   }
1190 
1191   // Comparing a SubI against a zero is equal to comparing the SubI
1192   // arguments directly.  This only works for eq and ne comparisons
1193   // due to possible integer overflow.
1194   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1195         (cop == Op_CmpI) &&
1196         (cmp1->Opcode() == Op_SubI) &&
1197         ( cmp2_type == TypeInt::ZERO ) ) {
1198     Node *ncmp = phase->transform( new CmpINode(cmp1->in(1),cmp1->in(2)));
1199     return new BoolNode( ncmp, _test._test );
1200   }
1201 
1202   // Change (-A vs 0) into (A vs 0) by commuting the test.  Disallow in the
1203   // most general case because negating 0x80000000 does nothing.  Needed for
1204   // the CmpF3/SubI/CmpI idiom.
1205   if( cop == Op_CmpI &&
1206       cmp1->Opcode() == Op_SubI &&
1207       cmp2_type == TypeInt::ZERO &&
1208       phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
1209       phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
1210     Node *ncmp = phase->transform( new CmpINode(cmp1->in(2),cmp2));
1211     return new BoolNode( ncmp, _test.commute() );
1212   }
1213 
1214   //  The transformation below is not valid for either signed or unsigned
1215   //  comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
1216   //  This transformation can be resurrected when we are able to
1217   //  make inferences about the range of values being subtracted from
1218   //  (or added to) relative to the wraparound point.
1219   //
1220   //    // Remove +/-1's if possible.
1221   //    // "X <= Y-1" becomes "X <  Y"
1222   //    // "X+1 <= Y" becomes "X <  Y"
1223   //    // "X <  Y+1" becomes "X <= Y"
1224   //    // "X-1 <  Y" becomes "X <= Y"
1225   //    // Do not this to compares off of the counted-loop-end.  These guys are
1226   //    // checking the trip counter and they want to use the post-incremented
1227   //    // counter.  If they use the PRE-incremented counter, then the counter has
1228   //    // to be incremented in a private block on a loop backedge.
1229   //    if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
1230   //      return NULL;
1231   //  #ifndef PRODUCT


src/share/vm/opto/subnode.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File