< prev index next >

src/share/vm/opto/divnode.cpp

Print this page




 440       Node *addend1 = phase->transform(new RShiftLNode(dividend, phase->intcon(N-1)));
 441 
 442       // If the divisor is negative, swap the order of the input addends;
 443       // this has the effect of negating the quotient.
 444       if (!d_pos) {
 445         Node *temp = addend0; addend0 = addend1; addend1 = temp;
 446       }
 447 
 448       // Adjust the final quotient by subtracting -1 (adding 1)
 449       // from the mul_hi.
 450       q = new SubLNode(addend0, addend1);
 451     }
 452   }
 453 
 454   return q;
 455 }
 456 
 457 //=============================================================================
 458 //------------------------------Identity---------------------------------------
 459 // If the divisor is 1, we are an identity on the dividend.
 460 Node *DivINode::Identity( PhaseTransform *phase ) {
 461   return (phase->type( in(2) )->higher_equal(TypeInt::ONE)) ? in(1) : this;
 462 }
 463 
 464 //------------------------------Idealize---------------------------------------
 465 // Divides can be changed to multiplies and/or shifts
 466 Node *DivINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 467   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 468   // Don't bother trying to transform a dead node
 469   if( in(0) && in(0)->is_top() )  return NULL;
 470 
 471   const Type *t = phase->type( in(2) );
 472   if( t == TypeInt::ONE )       // Identity?
 473     return NULL;                // Skip it
 474 
 475   const TypeInt *ti = t->isa_int();
 476   if( !ti ) return NULL;
 477   if( !ti->is_con() ) return NULL;
 478   jint i = ti->get_con();       // Get divisor
 479 
 480   if (i == 0) return NULL;      // Dividing by zero constant does not idealize
 481 
 482   if (in(0) != NULL) {
 483     phase->igvn_rehash_node_delayed(this);
 484     set_req(0, NULL);           // Dividing by a not-zero constant; no faulting
 485   }
 486 
 487   // Dividing by MININT does not optimize as a power-of-2 shift.
 488   if( i == min_jint ) return NULL;
 489 
 490   return transform_int_divide( phase, in(1), i );
 491 }
 492 
 493 //------------------------------Value------------------------------------------
 494 // A DivINode divides its inputs.  The third input is a Control input, used to
 495 // prevent hoisting the divide above an unsafe test.
 496 const Type *DivINode::Value( PhaseTransform *phase ) const {
 497   // Either input is TOP ==> the result is TOP
 498   const Type *t1 = phase->type( in(1) );
 499   const Type *t2 = phase->type( in(2) );
 500   if( t1 == Type::TOP ) return Type::TOP;
 501   if( t2 == Type::TOP ) return Type::TOP;
 502 
 503   // x/x == 1 since we always generate the dynamic divisor check for 0.
 504   if( phase->eqv( in(1), in(2) ) )
 505     return TypeInt::ONE;
 506 
 507   // Either input is BOTTOM ==> the result is the local BOTTOM
 508   const Type *bot = bottom_type();
 509   if( (t1 == bot) || (t2 == bot) ||
 510       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 511     return bot;
 512 
 513   // Divide the two numbers.  We approximate.
 514   // If divisor is a constant and not zero
 515   const TypeInt *i1 = t1->is_int();
 516   const TypeInt *i2 = t2->is_int();


 542     int32_t d = i1->get_con();
 543     if( d < 0 ) {
 544       if( d == min_jint ) {
 545         //  (-min_jint) == min_jint == (min_jint / -1)
 546         return TypeInt::make(min_jint, max_jint/2 + 1, widen);
 547       } else {
 548         return TypeInt::make(d, -d, widen);
 549       }
 550     }
 551     return TypeInt::make(-d, d, widen);
 552   }
 553 
 554   // Otherwise we give up all hope
 555   return TypeInt::INT;
 556 }
 557 
 558 
 559 //=============================================================================
 560 //------------------------------Identity---------------------------------------
 561 // If the divisor is 1, we are an identity on the dividend.
 562 Node *DivLNode::Identity( PhaseTransform *phase ) {
 563   return (phase->type( in(2) )->higher_equal(TypeLong::ONE)) ? in(1) : this;
 564 }
 565 
 566 //------------------------------Idealize---------------------------------------
 567 // Dividing by a power of 2 is a shift.
 568 Node *DivLNode::Ideal( PhaseGVN *phase, bool can_reshape) {
 569   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 570   // Don't bother trying to transform a dead node
 571   if( in(0) && in(0)->is_top() )  return NULL;
 572 
 573   const Type *t = phase->type( in(2) );
 574   if( t == TypeLong::ONE )      // Identity?
 575     return NULL;                // Skip it
 576 
 577   const TypeLong *tl = t->isa_long();
 578   if( !tl ) return NULL;
 579   if( !tl->is_con() ) return NULL;
 580   jlong l = tl->get_con();      // Get divisor
 581 
 582   if (l == 0) return NULL;      // Dividing by zero constant does not idealize
 583 
 584   if (in(0) != NULL) {
 585     phase->igvn_rehash_node_delayed(this);
 586     set_req(0, NULL);           // Dividing by a not-zero constant; no faulting
 587   }
 588 
 589   // Dividing by MINLONG does not optimize as a power-of-2 shift.
 590   if( l == min_jlong ) return NULL;
 591 
 592   return transform_long_divide( phase, in(1), l );
 593 }
 594 
 595 //------------------------------Value------------------------------------------
 596 // A DivLNode divides its inputs.  The third input is a Control input, used to
 597 // prevent hoisting the divide above an unsafe test.
 598 const Type *DivLNode::Value( PhaseTransform *phase ) const {
 599   // Either input is TOP ==> the result is TOP
 600   const Type *t1 = phase->type( in(1) );
 601   const Type *t2 = phase->type( in(2) );
 602   if( t1 == Type::TOP ) return Type::TOP;
 603   if( t2 == Type::TOP ) return Type::TOP;
 604 
 605   // x/x == 1 since we always generate the dynamic divisor check for 0.
 606   if( phase->eqv( in(1), in(2) ) )
 607     return TypeLong::ONE;
 608 
 609   // Either input is BOTTOM ==> the result is the local BOTTOM
 610   const Type *bot = bottom_type();
 611   if( (t1 == bot) || (t2 == bot) ||
 612       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 613     return bot;
 614 
 615   // Divide the two numbers.  We approximate.
 616   // If divisor is a constant and not zero
 617   const TypeLong *i1 = t1->is_long();
 618   const TypeLong *i2 = t2->is_long();


 645     if( d < 0 ) {
 646       if( d == min_jlong ) {
 647         //  (-min_jlong) == min_jlong == (min_jlong / -1)
 648         return TypeLong::make(min_jlong, max_jlong/2 + 1, widen);
 649       } else {
 650         return TypeLong::make(d, -d, widen);
 651       }
 652     }
 653     return TypeLong::make(-d, d, widen);
 654   }
 655 
 656   // Otherwise we give up all hope
 657   return TypeLong::LONG;
 658 }
 659 
 660 
 661 //=============================================================================
 662 //------------------------------Value------------------------------------------
 663 // An DivFNode divides its inputs.  The third input is a Control input, used to
 664 // prevent hoisting the divide above an unsafe test.
 665 const Type *DivFNode::Value( PhaseTransform *phase ) const {
 666   // Either input is TOP ==> the result is TOP
 667   const Type *t1 = phase->type( in(1) );
 668   const Type *t2 = phase->type( in(2) );
 669   if( t1 == Type::TOP ) return Type::TOP;
 670   if( t2 == Type::TOP ) return Type::TOP;
 671 
 672   // Either input is BOTTOM ==> the result is the local BOTTOM
 673   const Type *bot = bottom_type();
 674   if( (t1 == bot) || (t2 == bot) ||
 675       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 676     return bot;
 677 
 678   // x/x == 1, we ignore 0/0.
 679   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 680   // Does not work for variables because of NaN's
 681   if( phase->eqv( in(1), in(2) ) && t1->base() == Type::FloatCon)
 682     if (!g_isnan(t1->getf()) && g_isfinite(t1->getf()) && t1->getf() != 0.0) // could be negative ZERO or NaN
 683       return TypeF::ONE;
 684 
 685   if( t2 == TypeF::ONE )


 688   // If divisor is a constant and not zero, divide them numbers
 689   if( t1->base() == Type::FloatCon &&
 690       t2->base() == Type::FloatCon &&
 691       t2->getf() != 0.0 ) // could be negative zero
 692     return TypeF::make( t1->getf()/t2->getf() );
 693 
 694   // If the dividend is a constant zero
 695   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 696   // Test TypeF::ZERO is not sufficient as it could be negative zero
 697 
 698   if( t1 == TypeF::ZERO && !g_isnan(t2->getf()) && t2->getf() != 0.0 )
 699     return TypeF::ZERO;
 700 
 701   // Otherwise we give up all hope
 702   return Type::FLOAT;
 703 }
 704 
 705 //------------------------------isA_Copy---------------------------------------
 706 // Dividing by self is 1.
 707 // If the divisor is 1, we are an identity on the dividend.
 708 Node *DivFNode::Identity( PhaseTransform *phase ) {
 709   return (phase->type( in(2) ) == TypeF::ONE) ? in(1) : this;
 710 }
 711 
 712 
 713 //------------------------------Idealize---------------------------------------
 714 Node *DivFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 715   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 716   // Don't bother trying to transform a dead node
 717   if( in(0) && in(0)->is_top() )  return NULL;
 718 
 719   const Type *t2 = phase->type( in(2) );
 720   if( t2 == TypeF::ONE )         // Identity?
 721     return NULL;                // Skip it
 722 
 723   const TypeF *tf = t2->isa_float_constant();
 724   if( !tf ) return NULL;
 725   if( tf->base() != Type::FloatCon ) return NULL;
 726 
 727   // Check for out of range values
 728   if( tf->is_nan() || !tf->is_finite() ) return NULL;


 733 
 734   // Only for special case of dividing by a power of 2
 735   if( frexp((double)f, &exp) != 0.5 ) return NULL;
 736 
 737   // Limit the range of acceptable exponents
 738   if( exp < -126 || exp > 126 ) return NULL;
 739 
 740   // Compute the reciprocal
 741   float reciprocal = ((float)1.0) / f;
 742 
 743   assert( frexp((double)reciprocal, &exp) == 0.5, "reciprocal should be power of 2" );
 744 
 745   // return multiplication by the reciprocal
 746   return (new MulFNode(in(1), phase->makecon(TypeF::make(reciprocal))));
 747 }
 748 
 749 //=============================================================================
 750 //------------------------------Value------------------------------------------
 751 // An DivDNode divides its inputs.  The third input is a Control input, used to
 752 // prevent hoisting the divide above an unsafe test.
 753 const Type *DivDNode::Value( PhaseTransform *phase ) const {
 754   // Either input is TOP ==> the result is TOP
 755   const Type *t1 = phase->type( in(1) );
 756   const Type *t2 = phase->type( in(2) );
 757   if( t1 == Type::TOP ) return Type::TOP;
 758   if( t2 == Type::TOP ) return Type::TOP;
 759 
 760   // Either input is BOTTOM ==> the result is the local BOTTOM
 761   const Type *bot = bottom_type();
 762   if( (t1 == bot) || (t2 == bot) ||
 763       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 764     return bot;
 765 
 766   // x/x == 1, we ignore 0/0.
 767   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 768   // Does not work for variables because of NaN's
 769   if( phase->eqv( in(1), in(2) ) && t1->base() == Type::DoubleCon)
 770     if (!g_isnan(t1->getd()) && g_isfinite(t1->getd()) && t1->getd() != 0.0) // could be negative ZERO or NaN
 771       return TypeD::ONE;
 772 
 773   if( t2 == TypeD::ONE )


 783       if( t1->base() == Type::DoubleCon &&
 784           t2->base() == Type::DoubleCon &&
 785           t2->getd() != 0.0 ) // could be negative zero
 786         return TypeD::make( t1->getd()/t2->getd() );
 787     }
 788 
 789   // If the dividend is a constant zero
 790   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 791   // Test TypeF::ZERO is not sufficient as it could be negative zero
 792   if( t1 == TypeD::ZERO && !g_isnan(t2->getd()) && t2->getd() != 0.0 )
 793     return TypeD::ZERO;
 794 
 795   // Otherwise we give up all hope
 796   return Type::DOUBLE;
 797 }
 798 
 799 
 800 //------------------------------isA_Copy---------------------------------------
 801 // Dividing by self is 1.
 802 // If the divisor is 1, we are an identity on the dividend.
 803 Node *DivDNode::Identity( PhaseTransform *phase ) {
 804   return (phase->type( in(2) ) == TypeD::ONE) ? in(1) : this;
 805 }
 806 
 807 //------------------------------Idealize---------------------------------------
 808 Node *DivDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 809   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 810   // Don't bother trying to transform a dead node
 811   if( in(0) && in(0)->is_top() )  return NULL;
 812 
 813   const Type *t2 = phase->type( in(2) );
 814   if( t2 == TypeD::ONE )         // Identity?
 815     return NULL;                // Skip it
 816 
 817   const TypeD *td = t2->isa_double_constant();
 818   if( !td ) return NULL;
 819   if( td->base() != Type::DoubleCon ) return NULL;
 820 
 821   // Check for out of range values
 822   if( td->is_nan() || !td->is_finite() ) return NULL;
 823 


 955       mult = phase->transform( new LShiftINode( divide, phase->intcon( log2_con ) ) );
 956     else
 957       mult = phase->transform( new MulINode( divide, phase->intcon( pos_con ) ) );
 958 
 959     // Finally, subtract the multiplied divided value from the original
 960     result = new SubINode( in(1), mult );
 961   }
 962 
 963   // Now remove the bogus extra edges used to keep things alive
 964   if (can_reshape) {
 965     phase->is_IterGVN()->remove_dead_node(hook);
 966   } else {
 967     hook->set_req(0, NULL);       // Just yank bogus edge during Parse phase
 968   }
 969 
 970   // return the value
 971   return result;
 972 }
 973 
 974 //------------------------------Value------------------------------------------
 975 const Type *ModINode::Value( PhaseTransform *phase ) const {
 976   // Either input is TOP ==> the result is TOP
 977   const Type *t1 = phase->type( in(1) );
 978   const Type *t2 = phase->type( in(2) );
 979   if( t1 == Type::TOP ) return Type::TOP;
 980   if( t2 == Type::TOP ) return Type::TOP;
 981 
 982   // We always generate the dynamic check for 0.
 983   // 0 MOD X is 0
 984   if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
 985   // X MOD X is 0
 986   if( phase->eqv( in(1), in(2) ) ) return TypeInt::ZERO;
 987 
 988   // Either input is BOTTOM ==> the result is the local BOTTOM
 989   const Type *bot = bottom_type();
 990   if( (t1 == bot) || (t2 == bot) ||
 991       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 992     return bot;
 993 
 994   const TypeInt *i1 = t1->is_int();
 995   const TypeInt *i2 = t2->is_int();


1128       mult = phase->transform( new LShiftLNode( divide, phase->intcon( log2_con ) ) );
1129     else
1130       mult = phase->transform( new MulLNode( divide, phase->longcon( pos_con ) ) );
1131 
1132     // Finally, subtract the multiplied divided value from the original
1133     result = new SubLNode( in(1), mult );
1134   }
1135 
1136   // Now remove the bogus extra edges used to keep things alive
1137   if (can_reshape) {
1138     phase->is_IterGVN()->remove_dead_node(hook);
1139   } else {
1140     hook->set_req(0, NULL);       // Just yank bogus edge during Parse phase
1141   }
1142 
1143   // return the value
1144   return result;
1145 }
1146 
1147 //------------------------------Value------------------------------------------
1148 const Type *ModLNode::Value( PhaseTransform *phase ) const {
1149   // Either input is TOP ==> the result is TOP
1150   const Type *t1 = phase->type( in(1) );
1151   const Type *t2 = phase->type( in(2) );
1152   if( t1 == Type::TOP ) return Type::TOP;
1153   if( t2 == Type::TOP ) return Type::TOP;
1154 
1155   // We always generate the dynamic check for 0.
1156   // 0 MOD X is 0
1157   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
1158   // X MOD X is 0
1159   if( phase->eqv( in(1), in(2) ) ) return TypeLong::ZERO;
1160 
1161   // Either input is BOTTOM ==> the result is the local BOTTOM
1162   const Type *bot = bottom_type();
1163   if( (t1 == bot) || (t2 == bot) ||
1164       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1165     return bot;
1166 
1167   const TypeLong *i1 = t1->is_long();
1168   const TypeLong *i2 = t2->is_long();
1169   if( !i1->is_con() || !i2->is_con() ) {
1170     if( i1->_lo >= CONST64(0) && i2->_lo >= CONST64(0) )
1171       return TypeLong::POS;
1172     // If both numbers are not constants, we know little.
1173     return TypeLong::LONG;
1174   }
1175   // Mod by zero?  Throw exception at runtime!
1176   if( !i2->get_con() ) return TypeLong::POS;
1177 
1178   // We must be modulo'ing 2 float constants.
1179   // Check for min_jint % '-1', result is defined to be '0'.
1180   if( i1->get_con() == min_jlong && i2->get_con() == -1 )
1181     return TypeLong::ZERO;
1182 
1183   return TypeLong::make( i1->get_con() % i2->get_con() );
1184 }
1185 
1186 
1187 //=============================================================================
1188 //------------------------------Value------------------------------------------
1189 const Type *ModFNode::Value( PhaseTransform *phase ) const {
1190   // Either input is TOP ==> the result is TOP
1191   const Type *t1 = phase->type( in(1) );
1192   const Type *t2 = phase->type( in(2) );
1193   if( t1 == Type::TOP ) return Type::TOP;
1194   if( t2 == Type::TOP ) return Type::TOP;
1195 
1196   // Either input is BOTTOM ==> the result is the local BOTTOM
1197   const Type *bot = bottom_type();
1198   if( (t1 == bot) || (t2 == bot) ||
1199       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1200     return bot;
1201 
1202   // If either number is not a constant, we know nothing.
1203   if ((t1->base() != Type::FloatCon) || (t2->base() != Type::FloatCon)) {
1204     return Type::FLOAT;         // note: x%x can be either NaN or 0
1205   }
1206 
1207   float f1 = t1->getf();
1208   float f2 = t2->getf();
1209   jint  x1 = jint_cast(f1);     // note:  *(int*)&f1, not just (int)f1


1213   if (g_isnan(f1))    return t1;
1214   if (g_isnan(f2))    return t2;
1215 
1216   // If an operand is infinity or the divisor is +/- zero, punt.
1217   if (!g_isfinite(f1) || !g_isfinite(f2) || x2 == 0 || x2 == min_jint)
1218     return Type::FLOAT;
1219 
1220   // We must be modulo'ing 2 float constants.
1221   // Make sure that the sign of the fmod is equal to the sign of the dividend
1222   jint xr = jint_cast(fmod(f1, f2));
1223   if ((x1 ^ xr) < 0) {
1224     xr ^= min_jint;
1225   }
1226 
1227   return TypeF::make(jfloat_cast(xr));
1228 }
1229 
1230 
1231 //=============================================================================
1232 //------------------------------Value------------------------------------------
1233 const Type *ModDNode::Value( PhaseTransform *phase ) const {
1234   // Either input is TOP ==> the result is TOP
1235   const Type *t1 = phase->type( in(1) );
1236   const Type *t2 = phase->type( in(2) );
1237   if( t1 == Type::TOP ) return Type::TOP;
1238   if( t2 == Type::TOP ) return Type::TOP;
1239 
1240   // Either input is BOTTOM ==> the result is the local BOTTOM
1241   const Type *bot = bottom_type();
1242   if( (t1 == bot) || (t2 == bot) ||
1243       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1244     return bot;
1245 
1246   // If either number is not a constant, we know nothing.
1247   if ((t1->base() != Type::DoubleCon) || (t2->base() != Type::DoubleCon)) {
1248     return Type::DOUBLE;        // note: x%x can be either NaN or 0
1249   }
1250 
1251   double f1 = t1->getd();
1252   double f2 = t2->getd();
1253   jlong  x1 = jlong_cast(f1);   // note:  *(long*)&f1, not just (long)f1




 440       Node *addend1 = phase->transform(new RShiftLNode(dividend, phase->intcon(N-1)));
 441 
 442       // If the divisor is negative, swap the order of the input addends;
 443       // this has the effect of negating the quotient.
 444       if (!d_pos) {
 445         Node *temp = addend0; addend0 = addend1; addend1 = temp;
 446       }
 447 
 448       // Adjust the final quotient by subtracting -1 (adding 1)
 449       // from the mul_hi.
 450       q = new SubLNode(addend0, addend1);
 451     }
 452   }
 453 
 454   return q;
 455 }
 456 
 457 //=============================================================================
 458 //------------------------------Identity---------------------------------------
 459 // If the divisor is 1, we are an identity on the dividend.
 460 Node* DivINode::Identity(PhaseGVN* phase) {
 461   return (phase->type( in(2) )->higher_equal(TypeInt::ONE)) ? in(1) : this;
 462 }
 463 
 464 //------------------------------Idealize---------------------------------------
 465 // Divides can be changed to multiplies and/or shifts
 466 Node *DivINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 467   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 468   // Don't bother trying to transform a dead node
 469   if( in(0) && in(0)->is_top() )  return NULL;
 470 
 471   const Type *t = phase->type( in(2) );
 472   if( t == TypeInt::ONE )       // Identity?
 473     return NULL;                // Skip it
 474 
 475   const TypeInt *ti = t->isa_int();
 476   if( !ti ) return NULL;
 477   if( !ti->is_con() ) return NULL;
 478   jint i = ti->get_con();       // Get divisor
 479 
 480   if (i == 0) return NULL;      // Dividing by zero constant does not idealize
 481 
 482   if (in(0) != NULL) {
 483     phase->igvn_rehash_node_delayed(this);
 484     set_req(0, NULL);           // Dividing by a not-zero constant; no faulting
 485   }
 486 
 487   // Dividing by MININT does not optimize as a power-of-2 shift.
 488   if( i == min_jint ) return NULL;
 489 
 490   return transform_int_divide( phase, in(1), i );
 491 }
 492 
 493 //------------------------------Value------------------------------------------
 494 // A DivINode divides its inputs.  The third input is a Control input, used to
 495 // prevent hoisting the divide above an unsafe test.
 496 const Type* DivINode::Value(PhaseGVN* phase) const {
 497   // Either input is TOP ==> the result is TOP
 498   const Type *t1 = phase->type( in(1) );
 499   const Type *t2 = phase->type( in(2) );
 500   if( t1 == Type::TOP ) return Type::TOP;
 501   if( t2 == Type::TOP ) return Type::TOP;
 502 
 503   // x/x == 1 since we always generate the dynamic divisor check for 0.
 504   if( phase->eqv( in(1), in(2) ) )
 505     return TypeInt::ONE;
 506 
 507   // Either input is BOTTOM ==> the result is the local BOTTOM
 508   const Type *bot = bottom_type();
 509   if( (t1 == bot) || (t2 == bot) ||
 510       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 511     return bot;
 512 
 513   // Divide the two numbers.  We approximate.
 514   // If divisor is a constant and not zero
 515   const TypeInt *i1 = t1->is_int();
 516   const TypeInt *i2 = t2->is_int();


 542     int32_t d = i1->get_con();
 543     if( d < 0 ) {
 544       if( d == min_jint ) {
 545         //  (-min_jint) == min_jint == (min_jint / -1)
 546         return TypeInt::make(min_jint, max_jint/2 + 1, widen);
 547       } else {
 548         return TypeInt::make(d, -d, widen);
 549       }
 550     }
 551     return TypeInt::make(-d, d, widen);
 552   }
 553 
 554   // Otherwise we give up all hope
 555   return TypeInt::INT;
 556 }
 557 
 558 
 559 //=============================================================================
 560 //------------------------------Identity---------------------------------------
 561 // If the divisor is 1, we are an identity on the dividend.
 562 Node* DivLNode::Identity(PhaseGVN* phase) {
 563   return (phase->type( in(2) )->higher_equal(TypeLong::ONE)) ? in(1) : this;
 564 }
 565 
 566 //------------------------------Idealize---------------------------------------
 567 // Dividing by a power of 2 is a shift.
 568 Node *DivLNode::Ideal( PhaseGVN *phase, bool can_reshape) {
 569   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 570   // Don't bother trying to transform a dead node
 571   if( in(0) && in(0)->is_top() )  return NULL;
 572 
 573   const Type *t = phase->type( in(2) );
 574   if( t == TypeLong::ONE )      // Identity?
 575     return NULL;                // Skip it
 576 
 577   const TypeLong *tl = t->isa_long();
 578   if( !tl ) return NULL;
 579   if( !tl->is_con() ) return NULL;
 580   jlong l = tl->get_con();      // Get divisor
 581 
 582   if (l == 0) return NULL;      // Dividing by zero constant does not idealize
 583 
 584   if (in(0) != NULL) {
 585     phase->igvn_rehash_node_delayed(this);
 586     set_req(0, NULL);           // Dividing by a not-zero constant; no faulting
 587   }
 588 
 589   // Dividing by MINLONG does not optimize as a power-of-2 shift.
 590   if( l == min_jlong ) return NULL;
 591 
 592   return transform_long_divide( phase, in(1), l );
 593 }
 594 
 595 //------------------------------Value------------------------------------------
 596 // A DivLNode divides its inputs.  The third input is a Control input, used to
 597 // prevent hoisting the divide above an unsafe test.
 598 const Type* DivLNode::Value(PhaseGVN* phase) const {
 599   // Either input is TOP ==> the result is TOP
 600   const Type *t1 = phase->type( in(1) );
 601   const Type *t2 = phase->type( in(2) );
 602   if( t1 == Type::TOP ) return Type::TOP;
 603   if( t2 == Type::TOP ) return Type::TOP;
 604 
 605   // x/x == 1 since we always generate the dynamic divisor check for 0.
 606   if( phase->eqv( in(1), in(2) ) )
 607     return TypeLong::ONE;
 608 
 609   // Either input is BOTTOM ==> the result is the local BOTTOM
 610   const Type *bot = bottom_type();
 611   if( (t1 == bot) || (t2 == bot) ||
 612       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 613     return bot;
 614 
 615   // Divide the two numbers.  We approximate.
 616   // If divisor is a constant and not zero
 617   const TypeLong *i1 = t1->is_long();
 618   const TypeLong *i2 = t2->is_long();


 645     if( d < 0 ) {
 646       if( d == min_jlong ) {
 647         //  (-min_jlong) == min_jlong == (min_jlong / -1)
 648         return TypeLong::make(min_jlong, max_jlong/2 + 1, widen);
 649       } else {
 650         return TypeLong::make(d, -d, widen);
 651       }
 652     }
 653     return TypeLong::make(-d, d, widen);
 654   }
 655 
 656   // Otherwise we give up all hope
 657   return TypeLong::LONG;
 658 }
 659 
 660 
 661 //=============================================================================
 662 //------------------------------Value------------------------------------------
 663 // An DivFNode divides its inputs.  The third input is a Control input, used to
 664 // prevent hoisting the divide above an unsafe test.
 665 const Type* DivFNode::Value(PhaseGVN* phase) const {
 666   // Either input is TOP ==> the result is TOP
 667   const Type *t1 = phase->type( in(1) );
 668   const Type *t2 = phase->type( in(2) );
 669   if( t1 == Type::TOP ) return Type::TOP;
 670   if( t2 == Type::TOP ) return Type::TOP;
 671 
 672   // Either input is BOTTOM ==> the result is the local BOTTOM
 673   const Type *bot = bottom_type();
 674   if( (t1 == bot) || (t2 == bot) ||
 675       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 676     return bot;
 677 
 678   // x/x == 1, we ignore 0/0.
 679   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 680   // Does not work for variables because of NaN's
 681   if( phase->eqv( in(1), in(2) ) && t1->base() == Type::FloatCon)
 682     if (!g_isnan(t1->getf()) && g_isfinite(t1->getf()) && t1->getf() != 0.0) // could be negative ZERO or NaN
 683       return TypeF::ONE;
 684 
 685   if( t2 == TypeF::ONE )


 688   // If divisor is a constant and not zero, divide them numbers
 689   if( t1->base() == Type::FloatCon &&
 690       t2->base() == Type::FloatCon &&
 691       t2->getf() != 0.0 ) // could be negative zero
 692     return TypeF::make( t1->getf()/t2->getf() );
 693 
 694   // If the dividend is a constant zero
 695   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 696   // Test TypeF::ZERO is not sufficient as it could be negative zero
 697 
 698   if( t1 == TypeF::ZERO && !g_isnan(t2->getf()) && t2->getf() != 0.0 )
 699     return TypeF::ZERO;
 700 
 701   // Otherwise we give up all hope
 702   return Type::FLOAT;
 703 }
 704 
 705 //------------------------------isA_Copy---------------------------------------
 706 // Dividing by self is 1.
 707 // If the divisor is 1, we are an identity on the dividend.
 708 Node* DivFNode::Identity(PhaseGVN* phase) {
 709   return (phase->type( in(2) ) == TypeF::ONE) ? in(1) : this;
 710 }
 711 
 712 
 713 //------------------------------Idealize---------------------------------------
 714 Node *DivFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 715   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 716   // Don't bother trying to transform a dead node
 717   if( in(0) && in(0)->is_top() )  return NULL;
 718 
 719   const Type *t2 = phase->type( in(2) );
 720   if( t2 == TypeF::ONE )         // Identity?
 721     return NULL;                // Skip it
 722 
 723   const TypeF *tf = t2->isa_float_constant();
 724   if( !tf ) return NULL;
 725   if( tf->base() != Type::FloatCon ) return NULL;
 726 
 727   // Check for out of range values
 728   if( tf->is_nan() || !tf->is_finite() ) return NULL;


 733 
 734   // Only for special case of dividing by a power of 2
 735   if( frexp((double)f, &exp) != 0.5 ) return NULL;
 736 
 737   // Limit the range of acceptable exponents
 738   if( exp < -126 || exp > 126 ) return NULL;
 739 
 740   // Compute the reciprocal
 741   float reciprocal = ((float)1.0) / f;
 742 
 743   assert( frexp((double)reciprocal, &exp) == 0.5, "reciprocal should be power of 2" );
 744 
 745   // return multiplication by the reciprocal
 746   return (new MulFNode(in(1), phase->makecon(TypeF::make(reciprocal))));
 747 }
 748 
 749 //=============================================================================
 750 //------------------------------Value------------------------------------------
 751 // An DivDNode divides its inputs.  The third input is a Control input, used to
 752 // prevent hoisting the divide above an unsafe test.
 753 const Type* DivDNode::Value(PhaseGVN* phase) const {
 754   // Either input is TOP ==> the result is TOP
 755   const Type *t1 = phase->type( in(1) );
 756   const Type *t2 = phase->type( in(2) );
 757   if( t1 == Type::TOP ) return Type::TOP;
 758   if( t2 == Type::TOP ) return Type::TOP;
 759 
 760   // Either input is BOTTOM ==> the result is the local BOTTOM
 761   const Type *bot = bottom_type();
 762   if( (t1 == bot) || (t2 == bot) ||
 763       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 764     return bot;
 765 
 766   // x/x == 1, we ignore 0/0.
 767   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 768   // Does not work for variables because of NaN's
 769   if( phase->eqv( in(1), in(2) ) && t1->base() == Type::DoubleCon)
 770     if (!g_isnan(t1->getd()) && g_isfinite(t1->getd()) && t1->getd() != 0.0) // could be negative ZERO or NaN
 771       return TypeD::ONE;
 772 
 773   if( t2 == TypeD::ONE )


 783       if( t1->base() == Type::DoubleCon &&
 784           t2->base() == Type::DoubleCon &&
 785           t2->getd() != 0.0 ) // could be negative zero
 786         return TypeD::make( t1->getd()/t2->getd() );
 787     }
 788 
 789   // If the dividend is a constant zero
 790   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 791   // Test TypeF::ZERO is not sufficient as it could be negative zero
 792   if( t1 == TypeD::ZERO && !g_isnan(t2->getd()) && t2->getd() != 0.0 )
 793     return TypeD::ZERO;
 794 
 795   // Otherwise we give up all hope
 796   return Type::DOUBLE;
 797 }
 798 
 799 
 800 //------------------------------isA_Copy---------------------------------------
 801 // Dividing by self is 1.
 802 // If the divisor is 1, we are an identity on the dividend.
 803 Node* DivDNode::Identity(PhaseGVN* phase) {
 804   return (phase->type( in(2) ) == TypeD::ONE) ? in(1) : this;
 805 }
 806 
 807 //------------------------------Idealize---------------------------------------
 808 Node *DivDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 809   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 810   // Don't bother trying to transform a dead node
 811   if( in(0) && in(0)->is_top() )  return NULL;
 812 
 813   const Type *t2 = phase->type( in(2) );
 814   if( t2 == TypeD::ONE )         // Identity?
 815     return NULL;                // Skip it
 816 
 817   const TypeD *td = t2->isa_double_constant();
 818   if( !td ) return NULL;
 819   if( td->base() != Type::DoubleCon ) return NULL;
 820 
 821   // Check for out of range values
 822   if( td->is_nan() || !td->is_finite() ) return NULL;
 823 


 955       mult = phase->transform( new LShiftINode( divide, phase->intcon( log2_con ) ) );
 956     else
 957       mult = phase->transform( new MulINode( divide, phase->intcon( pos_con ) ) );
 958 
 959     // Finally, subtract the multiplied divided value from the original
 960     result = new SubINode( in(1), mult );
 961   }
 962 
 963   // Now remove the bogus extra edges used to keep things alive
 964   if (can_reshape) {
 965     phase->is_IterGVN()->remove_dead_node(hook);
 966   } else {
 967     hook->set_req(0, NULL);       // Just yank bogus edge during Parse phase
 968   }
 969 
 970   // return the value
 971   return result;
 972 }
 973 
 974 //------------------------------Value------------------------------------------
 975 const Type* ModINode::Value(PhaseGVN* phase) const {
 976   // Either input is TOP ==> the result is TOP
 977   const Type *t1 = phase->type( in(1) );
 978   const Type *t2 = phase->type( in(2) );
 979   if( t1 == Type::TOP ) return Type::TOP;
 980   if( t2 == Type::TOP ) return Type::TOP;
 981 
 982   // We always generate the dynamic check for 0.
 983   // 0 MOD X is 0
 984   if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
 985   // X MOD X is 0
 986   if( phase->eqv( in(1), in(2) ) ) return TypeInt::ZERO;
 987 
 988   // Either input is BOTTOM ==> the result is the local BOTTOM
 989   const Type *bot = bottom_type();
 990   if( (t1 == bot) || (t2 == bot) ||
 991       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 992     return bot;
 993 
 994   const TypeInt *i1 = t1->is_int();
 995   const TypeInt *i2 = t2->is_int();


1128       mult = phase->transform( new LShiftLNode( divide, phase->intcon( log2_con ) ) );
1129     else
1130       mult = phase->transform( new MulLNode( divide, phase->longcon( pos_con ) ) );
1131 
1132     // Finally, subtract the multiplied divided value from the original
1133     result = new SubLNode( in(1), mult );
1134   }
1135 
1136   // Now remove the bogus extra edges used to keep things alive
1137   if (can_reshape) {
1138     phase->is_IterGVN()->remove_dead_node(hook);
1139   } else {
1140     hook->set_req(0, NULL);       // Just yank bogus edge during Parse phase
1141   }
1142 
1143   // return the value
1144   return result;
1145 }
1146 
1147 //------------------------------Value------------------------------------------
1148 const Type* ModLNode::Value(PhaseGVN* phase) const {
1149   // Either input is TOP ==> the result is TOP
1150   const Type *t1 = phase->type( in(1) );
1151   const Type *t2 = phase->type( in(2) );
1152   if( t1 == Type::TOP ) return Type::TOP;
1153   if( t2 == Type::TOP ) return Type::TOP;
1154 
1155   // We always generate the dynamic check for 0.
1156   // 0 MOD X is 0
1157   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
1158   // X MOD X is 0
1159   if( phase->eqv( in(1), in(2) ) ) return TypeLong::ZERO;
1160 
1161   // Either input is BOTTOM ==> the result is the local BOTTOM
1162   const Type *bot = bottom_type();
1163   if( (t1 == bot) || (t2 == bot) ||
1164       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1165     return bot;
1166 
1167   const TypeLong *i1 = t1->is_long();
1168   const TypeLong *i2 = t2->is_long();
1169   if( !i1->is_con() || !i2->is_con() ) {
1170     if( i1->_lo >= CONST64(0) && i2->_lo >= CONST64(0) )
1171       return TypeLong::POS;
1172     // If both numbers are not constants, we know little.
1173     return TypeLong::LONG;
1174   }
1175   // Mod by zero?  Throw exception at runtime!
1176   if( !i2->get_con() ) return TypeLong::POS;
1177 
1178   // We must be modulo'ing 2 float constants.
1179   // Check for min_jint % '-1', result is defined to be '0'.
1180   if( i1->get_con() == min_jlong && i2->get_con() == -1 )
1181     return TypeLong::ZERO;
1182 
1183   return TypeLong::make( i1->get_con() % i2->get_con() );
1184 }
1185 
1186 
1187 //=============================================================================
1188 //------------------------------Value------------------------------------------
1189 const Type* ModFNode::Value(PhaseGVN* phase) const {
1190   // Either input is TOP ==> the result is TOP
1191   const Type *t1 = phase->type( in(1) );
1192   const Type *t2 = phase->type( in(2) );
1193   if( t1 == Type::TOP ) return Type::TOP;
1194   if( t2 == Type::TOP ) return Type::TOP;
1195 
1196   // Either input is BOTTOM ==> the result is the local BOTTOM
1197   const Type *bot = bottom_type();
1198   if( (t1 == bot) || (t2 == bot) ||
1199       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1200     return bot;
1201 
1202   // If either number is not a constant, we know nothing.
1203   if ((t1->base() != Type::FloatCon) || (t2->base() != Type::FloatCon)) {
1204     return Type::FLOAT;         // note: x%x can be either NaN or 0
1205   }
1206 
1207   float f1 = t1->getf();
1208   float f2 = t2->getf();
1209   jint  x1 = jint_cast(f1);     // note:  *(int*)&f1, not just (int)f1


1213   if (g_isnan(f1))    return t1;
1214   if (g_isnan(f2))    return t2;
1215 
1216   // If an operand is infinity or the divisor is +/- zero, punt.
1217   if (!g_isfinite(f1) || !g_isfinite(f2) || x2 == 0 || x2 == min_jint)
1218     return Type::FLOAT;
1219 
1220   // We must be modulo'ing 2 float constants.
1221   // Make sure that the sign of the fmod is equal to the sign of the dividend
1222   jint xr = jint_cast(fmod(f1, f2));
1223   if ((x1 ^ xr) < 0) {
1224     xr ^= min_jint;
1225   }
1226 
1227   return TypeF::make(jfloat_cast(xr));
1228 }
1229 
1230 
1231 //=============================================================================
1232 //------------------------------Value------------------------------------------
1233 const Type* ModDNode::Value(PhaseGVN* phase) const {
1234   // Either input is TOP ==> the result is TOP
1235   const Type *t1 = phase->type( in(1) );
1236   const Type *t2 = phase->type( in(2) );
1237   if( t1 == Type::TOP ) return Type::TOP;
1238   if( t2 == Type::TOP ) return Type::TOP;
1239 
1240   // Either input is BOTTOM ==> the result is the local BOTTOM
1241   const Type *bot = bottom_type();
1242   if( (t1 == bot) || (t2 == bot) ||
1243       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1244     return bot;
1245 
1246   // If either number is not a constant, we know nothing.
1247   if ((t1->base() != Type::DoubleCon) || (t2->base() != Type::DoubleCon)) {
1248     return Type::DOUBLE;        // note: x%x can be either NaN or 0
1249   }
1250 
1251   double f1 = t1->getd();
1252   double f2 = t2->getd();
1253   jlong  x1 = jlong_cast(f1);   // note:  *(long*)&f1, not just (long)f1


< prev index next >