< prev index next >

src/share/vm/opto/type.cpp

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


1353 // Only happens for pessimistic optimizations.
1354 const Type *TypeInt::narrow( const Type *old ) const {
1355   if (_lo >= _hi)  return this;   // already narrow enough
1356   if (old == NULL)  return this;
1357   const TypeInt* ot = old->isa_int();
1358   if (ot == NULL)  return this;
1359   jint olo = ot->_lo;
1360   jint ohi = ot->_hi;
1361 
1362   // If new guy is equal to old guy, no narrowing
1363   if (_lo == olo && _hi == ohi)  return old;
1364 
1365   // If old guy was maximum range, allow the narrowing
1366   if (olo == min_jint && ohi == max_jint)  return this;
1367 
1368   if (_lo < olo || _hi > ohi)
1369     return this;                // doesn't narrow; pretty wierd
1370 
1371   // The new type narrows the old type, so look for a "death march".
1372   // See comments on PhaseTransform::saturate.
1373   juint nrange = _hi - _lo;
1374   juint orange = ohi - olo;
1375   if (nrange < max_juint - 1 && nrange > (orange >> 1) + (SMALLINT*2)) {
1376     // Use the new type only if the range shrinks a lot.
1377     // We do not want the optimizer computing 2^31 point by point.
1378     return old;
1379   }
1380 
1381   return this;
1382 }
1383 
1384 //-----------------------------filter------------------------------------------
1385 const Type *TypeInt::filter_helper(const Type *kills, bool include_speculative) const {
1386   const TypeInt* ft = join_helper(kills, include_speculative)->isa_int();
1387   if (ft == NULL || ft->empty())
1388     return Type::TOP;           // Canonical empty value
1389   if (ft->_widen < this->_widen) {
1390     // Do not allow the value of kill->_widen to affect the outcome.
1391     // The widen bits must be allowed to run freely through the graph.
1392     ft = TypeInt::make(ft->_lo, ft->_hi, this->_widen);
1393   }
1394   return ft;
1395 }
1396 
1397 //------------------------------eq---------------------------------------------
1398 // Structural equality check for Type representations
1399 bool TypeInt::eq( const Type *t ) const {
1400   const TypeInt *r = t->is_int(); // Handy access
1401   return r->_lo == _lo && r->_hi == _hi && r->_widen == _widen;
1402 }
1403 
1404 //------------------------------hash-------------------------------------------
1405 // Type-specific hashing function.
1406 int TypeInt::hash(void) const {
1407   return _lo+_hi+_widen+(int)Type::Int;
1408 }
1409 
1410 //------------------------------is_finite--------------------------------------
1411 // Has a finite value
1412 bool TypeInt::is_finite() const {
1413   return true;
1414 }
1415 
1416 //------------------------------dump2------------------------------------------
1417 // Dump TypeInt
1418 #ifndef PRODUCT
1419 static const char* intname(char* buf, jint n) {
1420   if (n == min_jint)
1421     return "min";
1422   else if (n < min_jint + 10000)
1423     sprintf(buf, "min+" INT32_FORMAT, n - min_jint);
1424   else if (n == max_jint)
1425     return "max";
1426   else if (n > max_jint - 10000)
1427     sprintf(buf, "max-" INT32_FORMAT, max_jint - n);


1568   // If new guy contains old, then we widened
1569   if( _lo <= ot->_lo && _hi >= ot->_hi ) {
1570     // New contains old
1571     // If new guy is already wider than old, no widening
1572     if( _widen > ot->_widen ) return this;
1573     // If old guy was a constant, do not bother
1574     if (ot->_lo == ot->_hi)  return this;
1575     // Now widen new guy.
1576     // Check for widening too far
1577     if (_widen == WidenMax) {
1578       jlong max = max_jlong;
1579       jlong min = min_jlong;
1580       if (limit->isa_long()) {
1581         max = limit->is_long()->_hi;
1582         min = limit->is_long()->_lo;
1583       }
1584       if (min < _lo && _hi < max) {
1585         // If neither endpoint is extremal yet, push out the endpoint
1586         // which is closer to its respective limit.
1587         if (_lo >= 0 ||                 // easy common case
1588             (julong)(_lo - min) >= (julong)(max - _hi)) {
1589           // Try to widen to an unsigned range type of 32/63 bits:
1590           if (max >= max_juint && _hi < max_juint)
1591             return make(_lo, max_juint, WidenMax);
1592           else
1593             return make(_lo, max, WidenMax);
1594         } else {
1595           return make(min, _hi, WidenMax);
1596         }
1597       }
1598       return TypeLong::LONG;
1599     }
1600     // Returned widened new guy
1601     return make(_lo,_hi,_widen+1);
1602   }
1603 
1604   // If old guy contains new, then we probably widened too far & dropped to
1605   // bottom.  Return the wider fellow.
1606   if ( ot->_lo <= _lo && ot->_hi >= _hi )
1607     return old;
1608 


2387   // It is possible to construct a negative offset during PhaseCCP
2388 
2389   return (int)offset;        // Sum valid offsets
2390 }
2391 
2392 //------------------------------add_offset-------------------------------------
2393 const TypePtr *TypePtr::add_offset( intptr_t offset ) const {
2394   return make(AnyPtr, _ptr, xadd_offset(offset), _speculative, _inline_depth);
2395 }
2396 
2397 //------------------------------eq---------------------------------------------
2398 // Structural equality check for Type representations
2399 bool TypePtr::eq( const Type *t ) const {
2400   const TypePtr *a = (const TypePtr*)t;
2401   return _ptr == a->ptr() && _offset == a->offset() && eq_speculative(a) && _inline_depth == a->_inline_depth;
2402 }
2403 
2404 //------------------------------hash-------------------------------------------
2405 // Type-specific hashing function.
2406 int TypePtr::hash(void) const {
2407   return _ptr + _offset + hash_speculative() + _inline_depth;
2408 ;
2409 }
2410 
2411 /**
2412  * Return same type without a speculative part
2413  */
2414 const Type* TypePtr::remove_speculative() const {
2415   if (_speculative == NULL) {
2416     return this;
2417   }
2418   assert(_inline_depth == InlineDepthTop || _inline_depth == InlineDepthBottom, "non speculative type shouldn't have inline depth");
2419   return make(AnyPtr, _ptr, _offset, NULL, _inline_depth);
2420 }
2421 
2422 /**
2423  * Return same type but drop speculative part if we know we won't use
2424  * it
2425  */
2426 const Type* TypePtr::cleanup_speculative() const {
2427   if (speculative() == NULL) {


3197 
3198 //------------------------------eq---------------------------------------------
3199 // Structural equality check for Type representations
3200 bool TypeOopPtr::eq( const Type *t ) const {
3201   const TypeOopPtr *a = (const TypeOopPtr*)t;
3202   if (_klass_is_exact != a->_klass_is_exact ||
3203       _instance_id != a->_instance_id)  return false;
3204   ciObject* one = const_oop();
3205   ciObject* two = a->const_oop();
3206   if (one == NULL || two == NULL) {
3207     return (one == two) && TypePtr::eq(t);
3208   } else {
3209     return one->equals(two) && TypePtr::eq(t);
3210   }
3211 }
3212 
3213 //------------------------------hash-------------------------------------------
3214 // Type-specific hashing function.
3215 int TypeOopPtr::hash(void) const {
3216   return
3217     (const_oop() ? const_oop()->hash() : 0) +
3218     _klass_is_exact +
3219     _instance_id +
3220     TypePtr::hash();
3221 }
3222 
3223 //------------------------------dump2------------------------------------------
3224 #ifndef PRODUCT
3225 void TypeOopPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
3226   st->print("oopptr:%s", ptr_msg[_ptr]);
3227   if( _klass_is_exact ) st->print(":exact");
3228   if( const_oop() ) st->print(INTPTR_FORMAT, p2i(const_oop()));
3229   switch( _offset ) {
3230   case OffsetTop: st->print("+top"); break;
3231   case OffsetBot: st->print("+any"); break;
3232   case         0: break;
3233   default:        st->print("+%d",_offset); break;
3234   }
3235   if (_instance_id == InstanceTop)
3236     st->print(",iid=top");
3237   else if (_instance_id != InstanceBot)
3238     st->print(",iid=%d",_instance_id);
3239 
3240   dump_inline_depth(st);


3807 
3808 //------------------------------xdual------------------------------------------
3809 // Dual: do NOT dual on klasses.  This means I do NOT understand the Java
3810 // inheritance mechanism.
3811 const Type *TypeInstPtr::xdual() const {
3812   return new TypeInstPtr(dual_ptr(), klass(), klass_is_exact(), const_oop(), dual_offset(), dual_instance_id(), dual_speculative(), dual_inline_depth());
3813 }
3814 
3815 //------------------------------eq---------------------------------------------
3816 // Structural equality check for Type representations
3817 bool TypeInstPtr::eq( const Type *t ) const {
3818   const TypeInstPtr *p = t->is_instptr();
3819   return
3820     klass()->equals(p->klass()) &&
3821     TypeOopPtr::eq(p);          // Check sub-type stuff
3822 }
3823 
3824 //------------------------------hash-------------------------------------------
3825 // Type-specific hashing function.
3826 int TypeInstPtr::hash(void) const {
3827   int hash = klass()->hash() + TypeOopPtr::hash();
3828   return hash;
3829 }
3830 
3831 //------------------------------dump2------------------------------------------
3832 // Dump oop Type
3833 #ifndef PRODUCT
3834 void TypeInstPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
3835   // Print the name of the klass.
3836   klass()->print_name_on(st);
3837 
3838   switch( _ptr ) {
3839   case Constant:
3840     // TO DO: Make CI print the hex address of the underlying oop.
3841     if (WizardMode || Verbose) {
3842       const_oop()->print_oop(st);
3843     }
3844   case BotPTR:
3845     if (!WizardMode && !Verbose) {
3846       if( _klass_is_exact ) st->print(":exact");
3847       break;


4725   assert( k != NULL, "Expect a non-NULL klass");
4726   assert(k->is_instance_klass() || k->is_array_klass(), "Incorrect type of klass oop");
4727   TypeKlassPtr *r =
4728     (TypeKlassPtr*)(new TypeKlassPtr(ptr, k, offset))->hashcons();
4729 
4730   return r;
4731 }
4732 
4733 //------------------------------eq---------------------------------------------
4734 // Structural equality check for Type representations
4735 bool TypeKlassPtr::eq( const Type *t ) const {
4736   const TypeKlassPtr *p = t->is_klassptr();
4737   return
4738     klass()->equals(p->klass()) &&
4739     TypePtr::eq(p);
4740 }
4741 
4742 //------------------------------hash-------------------------------------------
4743 // Type-specific hashing function.
4744 int TypeKlassPtr::hash(void) const {
4745   return klass()->hash() + TypePtr::hash();
4746 }
4747 
4748 //------------------------------singleton--------------------------------------
4749 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
4750 // constants
4751 bool TypeKlassPtr::singleton(void) const {
4752   // detune optimizer to not generate constant klass + constant offset as a constant!
4753   // TopPTR, Null, AnyNull, Constant are all singletons
4754   return (_offset == 0) && !below_centerline(_ptr);
4755 }
4756 
4757 // Do not allow interface-vs.-noninterface joins to collapse to top.
4758 const Type *TypeKlassPtr::filter_helper(const Type *kills, bool include_speculative) const {
4759   // logic here mirrors the one from TypeOopPtr::filter. See comments
4760   // there.
4761   const Type* ft = join_helper(kills, include_speculative);
4762   const TypeKlassPtr* ftkp = ft->isa_klassptr();
4763   const TypeKlassPtr* ktkp = kills->isa_klassptr();
4764 
4765   if (ft->empty()) {




1353 // Only happens for pessimistic optimizations.
1354 const Type *TypeInt::narrow( const Type *old ) const {
1355   if (_lo >= _hi)  return this;   // already narrow enough
1356   if (old == NULL)  return this;
1357   const TypeInt* ot = old->isa_int();
1358   if (ot == NULL)  return this;
1359   jint olo = ot->_lo;
1360   jint ohi = ot->_hi;
1361 
1362   // If new guy is equal to old guy, no narrowing
1363   if (_lo == olo && _hi == ohi)  return old;
1364 
1365   // If old guy was maximum range, allow the narrowing
1366   if (olo == min_jint && ohi == max_jint)  return this;
1367 
1368   if (_lo < olo || _hi > ohi)
1369     return this;                // doesn't narrow; pretty wierd
1370 
1371   // The new type narrows the old type, so look for a "death march".
1372   // See comments on PhaseTransform::saturate.
1373   juint nrange = (juint)_hi - _lo;
1374   juint orange = (juint)ohi - olo;
1375   if (nrange < max_juint - 1 && nrange > (orange >> 1) + (SMALLINT*2)) {
1376     // Use the new type only if the range shrinks a lot.
1377     // We do not want the optimizer computing 2^31 point by point.
1378     return old;
1379   }
1380 
1381   return this;
1382 }
1383 
1384 //-----------------------------filter------------------------------------------
1385 const Type *TypeInt::filter_helper(const Type *kills, bool include_speculative) const {
1386   const TypeInt* ft = join_helper(kills, include_speculative)->isa_int();
1387   if (ft == NULL || ft->empty())
1388     return Type::TOP;           // Canonical empty value
1389   if (ft->_widen < this->_widen) {
1390     // Do not allow the value of kill->_widen to affect the outcome.
1391     // The widen bits must be allowed to run freely through the graph.
1392     ft = TypeInt::make(ft->_lo, ft->_hi, this->_widen);
1393   }
1394   return ft;
1395 }
1396 
1397 //------------------------------eq---------------------------------------------
1398 // Structural equality check for Type representations
1399 bool TypeInt::eq( const Type *t ) const {
1400   const TypeInt *r = t->is_int(); // Handy access
1401   return r->_lo == _lo && r->_hi == _hi && r->_widen == _widen;
1402 }
1403 
1404 //------------------------------hash-------------------------------------------
1405 // Type-specific hashing function.
1406 int TypeInt::hash(void) const {
1407   return java_add(java_add(_lo, _hi), java_add(_widen, (int)Type::Int));
1408 }
1409 
1410 //------------------------------is_finite--------------------------------------
1411 // Has a finite value
1412 bool TypeInt::is_finite() const {
1413   return true;
1414 }
1415 
1416 //------------------------------dump2------------------------------------------
1417 // Dump TypeInt
1418 #ifndef PRODUCT
1419 static const char* intname(char* buf, jint n) {
1420   if (n == min_jint)
1421     return "min";
1422   else if (n < min_jint + 10000)
1423     sprintf(buf, "min+" INT32_FORMAT, n - min_jint);
1424   else if (n == max_jint)
1425     return "max";
1426   else if (n > max_jint - 10000)
1427     sprintf(buf, "max-" INT32_FORMAT, max_jint - n);


1568   // If new guy contains old, then we widened
1569   if( _lo <= ot->_lo && _hi >= ot->_hi ) {
1570     // New contains old
1571     // If new guy is already wider than old, no widening
1572     if( _widen > ot->_widen ) return this;
1573     // If old guy was a constant, do not bother
1574     if (ot->_lo == ot->_hi)  return this;
1575     // Now widen new guy.
1576     // Check for widening too far
1577     if (_widen == WidenMax) {
1578       jlong max = max_jlong;
1579       jlong min = min_jlong;
1580       if (limit->isa_long()) {
1581         max = limit->is_long()->_hi;
1582         min = limit->is_long()->_lo;
1583       }
1584       if (min < _lo && _hi < max) {
1585         // If neither endpoint is extremal yet, push out the endpoint
1586         // which is closer to its respective limit.
1587         if (_lo >= 0 ||                 // easy common case
1588             ((julong)_lo - min) >= ((julong)max - _hi)) {
1589           // Try to widen to an unsigned range type of 32/63 bits:
1590           if (max >= max_juint && _hi < max_juint)
1591             return make(_lo, max_juint, WidenMax);
1592           else
1593             return make(_lo, max, WidenMax);
1594         } else {
1595           return make(min, _hi, WidenMax);
1596         }
1597       }
1598       return TypeLong::LONG;
1599     }
1600     // Returned widened new guy
1601     return make(_lo,_hi,_widen+1);
1602   }
1603 
1604   // If old guy contains new, then we probably widened too far & dropped to
1605   // bottom.  Return the wider fellow.
1606   if ( ot->_lo <= _lo && ot->_hi >= _hi )
1607     return old;
1608 


2387   // It is possible to construct a negative offset during PhaseCCP
2388 
2389   return (int)offset;        // Sum valid offsets
2390 }
2391 
2392 //------------------------------add_offset-------------------------------------
2393 const TypePtr *TypePtr::add_offset( intptr_t offset ) const {
2394   return make(AnyPtr, _ptr, xadd_offset(offset), _speculative, _inline_depth);
2395 }
2396 
2397 //------------------------------eq---------------------------------------------
2398 // Structural equality check for Type representations
2399 bool TypePtr::eq( const Type *t ) const {
2400   const TypePtr *a = (const TypePtr*)t;
2401   return _ptr == a->ptr() && _offset == a->offset() && eq_speculative(a) && _inline_depth == a->_inline_depth;
2402 }
2403 
2404 //------------------------------hash-------------------------------------------
2405 // Type-specific hashing function.
2406 int TypePtr::hash(void) const {
2407   return java_add(java_add(_ptr, _offset), java_add( hash_speculative(), _inline_depth));
2408 ;
2409 }
2410 
2411 /**
2412  * Return same type without a speculative part
2413  */
2414 const Type* TypePtr::remove_speculative() const {
2415   if (_speculative == NULL) {
2416     return this;
2417   }
2418   assert(_inline_depth == InlineDepthTop || _inline_depth == InlineDepthBottom, "non speculative type shouldn't have inline depth");
2419   return make(AnyPtr, _ptr, _offset, NULL, _inline_depth);
2420 }
2421 
2422 /**
2423  * Return same type but drop speculative part if we know we won't use
2424  * it
2425  */
2426 const Type* TypePtr::cleanup_speculative() const {
2427   if (speculative() == NULL) {


3197 
3198 //------------------------------eq---------------------------------------------
3199 // Structural equality check for Type representations
3200 bool TypeOopPtr::eq( const Type *t ) const {
3201   const TypeOopPtr *a = (const TypeOopPtr*)t;
3202   if (_klass_is_exact != a->_klass_is_exact ||
3203       _instance_id != a->_instance_id)  return false;
3204   ciObject* one = const_oop();
3205   ciObject* two = a->const_oop();
3206   if (one == NULL || two == NULL) {
3207     return (one == two) && TypePtr::eq(t);
3208   } else {
3209     return one->equals(two) && TypePtr::eq(t);
3210   }
3211 }
3212 
3213 //------------------------------hash-------------------------------------------
3214 // Type-specific hashing function.
3215 int TypeOopPtr::hash(void) const {
3216   return
3217     java_add(java_add(const_oop() ? const_oop()->hash() : 0, _klass_is_exact),
3218               java_add(_instance_id, TypePtr::hash()));


3219 }
3220 
3221 //------------------------------dump2------------------------------------------
3222 #ifndef PRODUCT
3223 void TypeOopPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
3224   st->print("oopptr:%s", ptr_msg[_ptr]);
3225   if( _klass_is_exact ) st->print(":exact");
3226   if( const_oop() ) st->print(INTPTR_FORMAT, p2i(const_oop()));
3227   switch( _offset ) {
3228   case OffsetTop: st->print("+top"); break;
3229   case OffsetBot: st->print("+any"); break;
3230   case         0: break;
3231   default:        st->print("+%d",_offset); break;
3232   }
3233   if (_instance_id == InstanceTop)
3234     st->print(",iid=top");
3235   else if (_instance_id != InstanceBot)
3236     st->print(",iid=%d",_instance_id);
3237 
3238   dump_inline_depth(st);


3805 
3806 //------------------------------xdual------------------------------------------
3807 // Dual: do NOT dual on klasses.  This means I do NOT understand the Java
3808 // inheritance mechanism.
3809 const Type *TypeInstPtr::xdual() const {
3810   return new TypeInstPtr(dual_ptr(), klass(), klass_is_exact(), const_oop(), dual_offset(), dual_instance_id(), dual_speculative(), dual_inline_depth());
3811 }
3812 
3813 //------------------------------eq---------------------------------------------
3814 // Structural equality check for Type representations
3815 bool TypeInstPtr::eq( const Type *t ) const {
3816   const TypeInstPtr *p = t->is_instptr();
3817   return
3818     klass()->equals(p->klass()) &&
3819     TypeOopPtr::eq(p);          // Check sub-type stuff
3820 }
3821 
3822 //------------------------------hash-------------------------------------------
3823 // Type-specific hashing function.
3824 int TypeInstPtr::hash(void) const {
3825   int hash = java_add(klass()->hash(), TypeOopPtr::hash());
3826   return hash;
3827 }
3828 
3829 //------------------------------dump2------------------------------------------
3830 // Dump oop Type
3831 #ifndef PRODUCT
3832 void TypeInstPtr::dump2( Dict &d, uint depth, outputStream *st ) const {
3833   // Print the name of the klass.
3834   klass()->print_name_on(st);
3835 
3836   switch( _ptr ) {
3837   case Constant:
3838     // TO DO: Make CI print the hex address of the underlying oop.
3839     if (WizardMode || Verbose) {
3840       const_oop()->print_oop(st);
3841     }
3842   case BotPTR:
3843     if (!WizardMode && !Verbose) {
3844       if( _klass_is_exact ) st->print(":exact");
3845       break;


4723   assert( k != NULL, "Expect a non-NULL klass");
4724   assert(k->is_instance_klass() || k->is_array_klass(), "Incorrect type of klass oop");
4725   TypeKlassPtr *r =
4726     (TypeKlassPtr*)(new TypeKlassPtr(ptr, k, offset))->hashcons();
4727 
4728   return r;
4729 }
4730 
4731 //------------------------------eq---------------------------------------------
4732 // Structural equality check for Type representations
4733 bool TypeKlassPtr::eq( const Type *t ) const {
4734   const TypeKlassPtr *p = t->is_klassptr();
4735   return
4736     klass()->equals(p->klass()) &&
4737     TypePtr::eq(p);
4738 }
4739 
4740 //------------------------------hash-------------------------------------------
4741 // Type-specific hashing function.
4742 int TypeKlassPtr::hash(void) const {
4743   return java_add(klass()->hash(), TypePtr::hash());
4744 }
4745 
4746 //------------------------------singleton--------------------------------------
4747 // TRUE if Type is a singleton type, FALSE otherwise.   Singletons are simple
4748 // constants
4749 bool TypeKlassPtr::singleton(void) const {
4750   // detune optimizer to not generate constant klass + constant offset as a constant!
4751   // TopPTR, Null, AnyNull, Constant are all singletons
4752   return (_offset == 0) && !below_centerline(_ptr);
4753 }
4754 
4755 // Do not allow interface-vs.-noninterface joins to collapse to top.
4756 const Type *TypeKlassPtr::filter_helper(const Type *kills, bool include_speculative) const {
4757   // logic here mirrors the one from TypeOopPtr::filter. See comments
4758   // there.
4759   const Type* ft = join_helper(kills, include_speculative);
4760   const TypeKlassPtr* ftkp = ft->isa_klassptr();
4761   const TypeKlassPtr* ktkp = kills->isa_klassptr();
4762 
4763   if (ft->empty()) {


< prev index next >