< prev index next >

src/share/vm/opto/mulnode.cpp

Print this page
rev 9644 : 8145096: Undefined behaviour in HotSpot
Summary: Fix some integer overflows
Reviewed-by: duke


 228   return res;                   // Return final result
 229 }
 230 
 231 //------------------------------mul_ring---------------------------------------
 232 // Compute the product type of two integer ranges into this node.
 233 const Type *MulINode::mul_ring(const Type *t0, const Type *t1) const {
 234   const TypeInt *r0 = t0->is_int(); // Handy access
 235   const TypeInt *r1 = t1->is_int();
 236 
 237   // Fetch endpoints of all ranges
 238   int32_t lo0 = r0->_lo;
 239   double a = (double)lo0;
 240   int32_t hi0 = r0->_hi;
 241   double b = (double)hi0;
 242   int32_t lo1 = r1->_lo;
 243   double c = (double)lo1;
 244   int32_t hi1 = r1->_hi;
 245   double d = (double)hi1;
 246 
 247   // Compute all endpoints & check for overflow
 248   int32_t A = lo0*lo1;
 249   if( (double)A != a*c ) return TypeInt::INT; // Overflow?
 250   int32_t B = lo0*hi1;
 251   if( (double)B != a*d ) return TypeInt::INT; // Overflow?
 252   int32_t C = hi0*lo1;
 253   if( (double)C != b*c ) return TypeInt::INT; // Overflow?
 254   int32_t D = hi0*hi1;
 255   if( (double)D != b*d ) return TypeInt::INT; // Overflow?
 256 
 257   if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
 258   else { lo0 = B; hi0 = A; }
 259   if( C < D ) {
 260     if( C < lo0 ) lo0 = C;
 261     if( D > hi0 ) hi0 = D;
 262   } else {
 263     if( D < lo0 ) lo0 = D;
 264     if( C > hi0 ) hi0 = C;
 265   }
 266   return TypeInt::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
 267 }
 268 
 269 
 270 //=============================================================================
 271 //------------------------------Ideal------------------------------------------
 272 // Check for power-of-2 multiply, then try the regular MulNode::Ideal
 273 Node *MulLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 274   // Swap constant to right


 324   return res;                   // Return final result
 325 }
 326 
 327 //------------------------------mul_ring---------------------------------------
 328 // Compute the product type of two integer ranges into this node.
 329 const Type *MulLNode::mul_ring(const Type *t0, const Type *t1) const {
 330   const TypeLong *r0 = t0->is_long(); // Handy access
 331   const TypeLong *r1 = t1->is_long();
 332 
 333   // Fetch endpoints of all ranges
 334   jlong lo0 = r0->_lo;
 335   double a = (double)lo0;
 336   jlong hi0 = r0->_hi;
 337   double b = (double)hi0;
 338   jlong lo1 = r1->_lo;
 339   double c = (double)lo1;
 340   jlong hi1 = r1->_hi;
 341   double d = (double)hi1;
 342 
 343   // Compute all endpoints & check for overflow
 344   jlong A = lo0*lo1;
 345   if( (double)A != a*c ) return TypeLong::LONG; // Overflow?
 346   jlong B = lo0*hi1;
 347   if( (double)B != a*d ) return TypeLong::LONG; // Overflow?
 348   jlong C = hi0*lo1;
 349   if( (double)C != b*c ) return TypeLong::LONG; // Overflow?
 350   jlong D = hi0*hi1;
 351   if( (double)D != b*d ) return TypeLong::LONG; // Overflow?
 352 
 353   if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
 354   else { lo0 = B; hi0 = A; }
 355   if( C < D ) {
 356     if( C < lo0 ) lo0 = C;
 357     if( D > hi0 ) hi0 = D;
 358   } else {
 359     if( D < lo0 ) lo0 = D;
 360     if( C > hi0 ) hi0 = C;
 361   }
 362   return TypeLong::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
 363 }
 364 
 365 //=============================================================================
 366 //------------------------------mul_ring---------------------------------------
 367 // Compute the product type of two double ranges into this node.
 368 const Type *MulFNode::mul_ring(const Type *t0, const Type *t1) const {
 369   if( t0 == Type::FLOAT || t1 == Type::FLOAT ) return Type::FLOAT;
 370   return TypeF::make( t0->getf() * t1->getf() );


 557   if( r1->is_con() && r1->get_con() > 0 )
 558     return TypeLong::make(CONST64(0), r1->get_con(), widen);
 559 
 560   return TypeLong::LONG;        // No constants to be had
 561 }
 562 
 563 //------------------------------Identity---------------------------------------
 564 // Masking off the high bits of an unsigned load is not required
 565 Node *AndLNode::Identity( PhaseTransform *phase ) {
 566 
 567   // x & x => x
 568   if (phase->eqv(in(1), in(2))) return in(1);
 569 
 570   Node *usr = in(1);
 571   const TypeLong *t2 = phase->type( in(2) )->isa_long();
 572   if( t2 && t2->is_con() ) {
 573     jlong con = t2->get_con();
 574     // Masking off high bits which are always zero is useless.
 575     const TypeLong* t1 = phase->type( in(1) )->isa_long();
 576     if (t1 != NULL && t1->_lo >= 0) {
 577       jlong t1_support = ((jlong)1 << (1 + log2_long(t1->_hi))) - 1;
 578       if ((t1_support & con) == t1_support)
 579         return usr;
 580     }
 581     uint lop = usr->Opcode();
 582     // Masking off the high bits of a unsigned-shift-right is not
 583     // needed either.
 584     if( lop == Op_URShiftL ) {
 585       const TypeInt *t12 = phase->type( usr->in(2) )->isa_int();
 586       if( t12 && t12->is_con() ) {  // Shift is by a constant
 587         int shift = t12->get_con();
 588         shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
 589         jlong mask = max_julong >> shift;
 590         if( (mask&con) == mask )  // If AND is useless, skip it
 591           return usr;
 592       }
 593     }
 594   }
 595   return MulNode::Identity(phase);
 596 }
 597 


 785   // Check for "(x>>c0)<<c0" which just masks off low bits
 786   if( (add1_op == Op_RShiftL || add1_op == Op_URShiftL ) &&
 787       add1->in(2) == in(2) )
 788     // Convert to "(x & -(1<<c0))"
 789     return new AndLNode(add1->in(1),phase->longcon( -(CONST64(1)<<con)));
 790 
 791   // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits
 792   if( add1_op == Op_AndL ) {
 793     Node *add2 = add1->in(1);
 794     int add2_op = add2->Opcode();
 795     if( (add2_op == Op_RShiftL || add2_op == Op_URShiftL ) &&
 796         add2->in(2) == in(2) ) {
 797       // Convert to "(x & (Y<<c0))"
 798       Node *y_sh = phase->transform( new LShiftLNode( add1->in(2), in(2) ) );
 799       return new AndLNode( add2->in(1), y_sh );
 800     }
 801   }
 802 
 803   // Check for ((x & ((CONST64(1)<<(64-c0))-1)) << c0) which ANDs off high bits
 804   // before shifting them away.
 805   const jlong bits_mask = ((jlong)CONST64(1) << (jlong)(BitsPerJavaLong - con)) - CONST64(1);
 806   if( add1_op == Op_AndL &&
 807       phase->type(add1->in(2)) == TypeLong::make( bits_mask ) )
 808     return new LShiftLNode( add1->in(1), in(2) );
 809 
 810   return NULL;
 811 }
 812 
 813 //------------------------------Value------------------------------------------
 814 // A LShiftLNode shifts its input2 left by input1 amount.
 815 const Type *LShiftLNode::Value( PhaseTransform *phase ) const {
 816   const Type *t1 = phase->type( in(1) );
 817   const Type *t2 = phase->type( in(2) );
 818   // Either input is TOP ==> the result is TOP
 819   if( t1 == Type::TOP ) return Type::TOP;
 820   if( t2 == Type::TOP ) return Type::TOP;
 821 
 822   // Left input is ZERO ==> the result is ZERO.
 823   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
 824   // Shift by zero does nothing
 825   if( t2 == TypeInt::ZERO ) return t1;


1237   // }
1238 
1239   return TypeInt::INT;
1240 }
1241 
1242 //=============================================================================
1243 //------------------------------Identity---------------------------------------
1244 Node *URShiftLNode::Identity( PhaseTransform *phase ) {
1245   const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int
1246   return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerLong - 1 ) ) == 0 ) ? in(1) : this;
1247 }
1248 
1249 //------------------------------Ideal------------------------------------------
1250 Node *URShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1251   const TypeInt *t2 = phase->type( in(2) )->isa_int();
1252   if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
1253   const int con = t2->get_con() & ( BitsPerLong - 1 ); // Shift count is always masked
1254   if ( con == 0 ) return NULL;  // let Identity() handle a 0 shift count
1255                               // note: mask computation below does not work for 0 shift count
1256   // We'll be wanting the right-shift amount as a mask of that many bits
1257   const jlong mask = (((jlong)CONST64(1) << (jlong)(BitsPerJavaLong - con)) -1);
1258 
1259   // Check for ((x << z) + Y) >>> z.  Replace with x + con>>>z
1260   // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
1261   // If Q is "X << z" the rounding is useless.  Look for patterns like
1262   // ((X<<Z) + Y) >>> Z  and replace with (X + Y>>>Z) & Z-mask.
1263   Node *add = in(1);
1264   if( add->Opcode() == Op_AddL ) {
1265     Node *lshl = add->in(1);
1266     if( lshl->Opcode() == Op_LShiftL &&
1267         phase->type(lshl->in(2)) == t2 ) {
1268       Node *y_z = phase->transform( new URShiftLNode(add->in(2),in(2)) );
1269       Node *sum = phase->transform( new AddLNode( lshl->in(1), y_z ) );
1270       return new AndLNode( sum, phase->longcon(mask) );
1271     }
1272   }
1273 
1274   // Check for (x & mask) >>> z.  Replace with (x >>> z) & (mask >>> z)
1275   // This shortens the mask.  Also, if we are extracting a high byte and
1276   // storing it to a buffer, the mask will be removed completely.
1277   Node *andi = in(1);




 228   return res;                   // Return final result
 229 }
 230 
 231 //------------------------------mul_ring---------------------------------------
 232 // Compute the product type of two integer ranges into this node.
 233 const Type *MulINode::mul_ring(const Type *t0, const Type *t1) const {
 234   const TypeInt *r0 = t0->is_int(); // Handy access
 235   const TypeInt *r1 = t1->is_int();
 236 
 237   // Fetch endpoints of all ranges
 238   int32_t lo0 = r0->_lo;
 239   double a = (double)lo0;
 240   int32_t hi0 = r0->_hi;
 241   double b = (double)hi0;
 242   int32_t lo1 = r1->_lo;
 243   double c = (double)lo1;
 244   int32_t hi1 = r1->_hi;
 245   double d = (double)hi1;
 246 
 247   // Compute all endpoints & check for overflow
 248   int32_t A = java_multiply(lo0, lo1);
 249   if( (double)A != a*c ) return TypeInt::INT; // Overflow?
 250   int32_t B = java_multiply(lo0, hi1);
 251   if( (double)B != a*d ) return TypeInt::INT; // Overflow?
 252   int32_t C = java_multiply(hi0, lo1);
 253   if( (double)C != b*c ) return TypeInt::INT; // Overflow?
 254   int32_t D = java_multiply(hi0, hi1);
 255   if( (double)D != b*d ) return TypeInt::INT; // Overflow?
 256 
 257   if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
 258   else { lo0 = B; hi0 = A; }
 259   if( C < D ) {
 260     if( C < lo0 ) lo0 = C;
 261     if( D > hi0 ) hi0 = D;
 262   } else {
 263     if( D < lo0 ) lo0 = D;
 264     if( C > hi0 ) hi0 = C;
 265   }
 266   return TypeInt::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
 267 }
 268 
 269 
 270 //=============================================================================
 271 //------------------------------Ideal------------------------------------------
 272 // Check for power-of-2 multiply, then try the regular MulNode::Ideal
 273 Node *MulLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 274   // Swap constant to right


 324   return res;                   // Return final result
 325 }
 326 
 327 //------------------------------mul_ring---------------------------------------
 328 // Compute the product type of two integer ranges into this node.
 329 const Type *MulLNode::mul_ring(const Type *t0, const Type *t1) const {
 330   const TypeLong *r0 = t0->is_long(); // Handy access
 331   const TypeLong *r1 = t1->is_long();
 332 
 333   // Fetch endpoints of all ranges
 334   jlong lo0 = r0->_lo;
 335   double a = (double)lo0;
 336   jlong hi0 = r0->_hi;
 337   double b = (double)hi0;
 338   jlong lo1 = r1->_lo;
 339   double c = (double)lo1;
 340   jlong hi1 = r1->_hi;
 341   double d = (double)hi1;
 342 
 343   // Compute all endpoints & check for overflow
 344   jlong A = java_multiply(lo0, lo1);
 345   if( (double)A != a*c ) return TypeLong::LONG; // Overflow?
 346   jlong B = java_multiply(lo0, hi1);
 347   if( (double)B != a*d ) return TypeLong::LONG; // Overflow?
 348   jlong C = java_multiply(hi0, lo1);
 349   if( (double)C != b*c ) return TypeLong::LONG; // Overflow?
 350   jlong D = java_multiply(hi0, hi1);
 351   if( (double)D != b*d ) return TypeLong::LONG; // Overflow?
 352 
 353   if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
 354   else { lo0 = B; hi0 = A; }
 355   if( C < D ) {
 356     if( C < lo0 ) lo0 = C;
 357     if( D > hi0 ) hi0 = D;
 358   } else {
 359     if( D < lo0 ) lo0 = D;
 360     if( C > hi0 ) hi0 = C;
 361   }
 362   return TypeLong::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
 363 }
 364 
 365 //=============================================================================
 366 //------------------------------mul_ring---------------------------------------
 367 // Compute the product type of two double ranges into this node.
 368 const Type *MulFNode::mul_ring(const Type *t0, const Type *t1) const {
 369   if( t0 == Type::FLOAT || t1 == Type::FLOAT ) return Type::FLOAT;
 370   return TypeF::make( t0->getf() * t1->getf() );


 557   if( r1->is_con() && r1->get_con() > 0 )
 558     return TypeLong::make(CONST64(0), r1->get_con(), widen);
 559 
 560   return TypeLong::LONG;        // No constants to be had
 561 }
 562 
 563 //------------------------------Identity---------------------------------------
 564 // Masking off the high bits of an unsigned load is not required
 565 Node *AndLNode::Identity( PhaseTransform *phase ) {
 566 
 567   // x & x => x
 568   if (phase->eqv(in(1), in(2))) return in(1);
 569 
 570   Node *usr = in(1);
 571   const TypeLong *t2 = phase->type( in(2) )->isa_long();
 572   if( t2 && t2->is_con() ) {
 573     jlong con = t2->get_con();
 574     // Masking off high bits which are always zero is useless.
 575     const TypeLong* t1 = phase->type( in(1) )->isa_long();
 576     if (t1 != NULL && t1->_lo >= 0) {
 577       jlong t1_support = java_subtract(((jlong)1 << (1 + log2_long(t1->_hi))), (jlong)1);
 578       if ((t1_support & con) == t1_support)
 579         return usr;
 580     }
 581     uint lop = usr->Opcode();
 582     // Masking off the high bits of a unsigned-shift-right is not
 583     // needed either.
 584     if( lop == Op_URShiftL ) {
 585       const TypeInt *t12 = phase->type( usr->in(2) )->isa_int();
 586       if( t12 && t12->is_con() ) {  // Shift is by a constant
 587         int shift = t12->get_con();
 588         shift &= BitsPerJavaLong - 1;  // semantics of Java shifts
 589         jlong mask = max_julong >> shift;
 590         if( (mask&con) == mask )  // If AND is useless, skip it
 591           return usr;
 592       }
 593     }
 594   }
 595   return MulNode::Identity(phase);
 596 }
 597 


 785   // Check for "(x>>c0)<<c0" which just masks off low bits
 786   if( (add1_op == Op_RShiftL || add1_op == Op_URShiftL ) &&
 787       add1->in(2) == in(2) )
 788     // Convert to "(x & -(1<<c0))"
 789     return new AndLNode(add1->in(1),phase->longcon( -(CONST64(1)<<con)));
 790 
 791   // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits
 792   if( add1_op == Op_AndL ) {
 793     Node *add2 = add1->in(1);
 794     int add2_op = add2->Opcode();
 795     if( (add2_op == Op_RShiftL || add2_op == Op_URShiftL ) &&
 796         add2->in(2) == in(2) ) {
 797       // Convert to "(x & (Y<<c0))"
 798       Node *y_sh = phase->transform( new LShiftLNode( add1->in(2), in(2) ) );
 799       return new AndLNode( add2->in(1), y_sh );
 800     }
 801   }
 802 
 803   // Check for ((x & ((CONST64(1)<<(64-c0))-1)) << c0) which ANDs off high bits
 804   // before shifting them away.
 805   const jlong bits_mask = java_subtract((jlong)CONST64(1) << (jlong)(BitsPerJavaLong - con), CONST64(1));
 806   if( add1_op == Op_AndL &&
 807       phase->type(add1->in(2)) == TypeLong::make( bits_mask ) )
 808     return new LShiftLNode( add1->in(1), in(2) );
 809 
 810   return NULL;
 811 }
 812 
 813 //------------------------------Value------------------------------------------
 814 // A LShiftLNode shifts its input2 left by input1 amount.
 815 const Type *LShiftLNode::Value( PhaseTransform *phase ) const {
 816   const Type *t1 = phase->type( in(1) );
 817   const Type *t2 = phase->type( in(2) );
 818   // Either input is TOP ==> the result is TOP
 819   if( t1 == Type::TOP ) return Type::TOP;
 820   if( t2 == Type::TOP ) return Type::TOP;
 821 
 822   // Left input is ZERO ==> the result is ZERO.
 823   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
 824   // Shift by zero does nothing
 825   if( t2 == TypeInt::ZERO ) return t1;


1237   // }
1238 
1239   return TypeInt::INT;
1240 }
1241 
1242 //=============================================================================
1243 //------------------------------Identity---------------------------------------
1244 Node *URShiftLNode::Identity( PhaseTransform *phase ) {
1245   const TypeInt *ti = phase->type( in(2) )->isa_int(); // shift count is an int
1246   return ( ti && ti->is_con() && ( ti->get_con() & ( BitsPerLong - 1 ) ) == 0 ) ? in(1) : this;
1247 }
1248 
1249 //------------------------------Ideal------------------------------------------
1250 Node *URShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1251   const TypeInt *t2 = phase->type( in(2) )->isa_int();
1252   if( !t2 || !t2->is_con() ) return NULL; // Right input is a constant
1253   const int con = t2->get_con() & ( BitsPerLong - 1 ); // Shift count is always masked
1254   if ( con == 0 ) return NULL;  // let Identity() handle a 0 shift count
1255                               // note: mask computation below does not work for 0 shift count
1256   // We'll be wanting the right-shift amount as a mask of that many bits
1257   const jlong mask = java_subtract(((jlong)CONST64(1) << (jlong)(BitsPerJavaLong - con)), (jlong)1);
1258 
1259   // Check for ((x << z) + Y) >>> z.  Replace with x + con>>>z
1260   // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
1261   // If Q is "X << z" the rounding is useless.  Look for patterns like
1262   // ((X<<Z) + Y) >>> Z  and replace with (X + Y>>>Z) & Z-mask.
1263   Node *add = in(1);
1264   if( add->Opcode() == Op_AddL ) {
1265     Node *lshl = add->in(1);
1266     if( lshl->Opcode() == Op_LShiftL &&
1267         phase->type(lshl->in(2)) == t2 ) {
1268       Node *y_z = phase->transform( new URShiftLNode(add->in(2),in(2)) );
1269       Node *sum = phase->transform( new AddLNode( lshl->in(1), y_z ) );
1270       return new AndLNode( sum, phase->longcon(mask) );
1271     }
1272   }
1273 
1274   // Check for (x & mask) >>> z.  Replace with (x >>> z) & (mask >>> z)
1275   // This shortens the mask.  Also, if we are extracting a high byte and
1276   // storing it to a buffer, the mask will be removed completely.
1277   Node *andi = in(1);


< prev index next >