RFR: 8240615: is_power_of_2() has Undefined Behaviour and is inconsistent

Andrew Haley aph at redhat.com
Fri Mar 6 14:48:42 UTC 2020


On 3/5/20 7:46 PM, Stefan Karlsson wrote:
> I ran with this patch:
> diff --git a/src/hotspot/share/utilities/powerOfTwo.hpp 
> b/src/hotspot/share/utilities/powerOfTwo.hpp
> --- a/src/hotspot/share/utilities/powerOfTwo.hpp
> +++ b/src/hotspot/share/utilities/powerOfTwo.hpp
> @@ -36,6 +36,7 @@
> 
>   template <typename T>
>   bool is_power_of_2(T x) {
> +  assert(sizeof(x) != 8 || !IsSigned<T>::value || x != 
> std::numeric_limits<T>::min(), "Caught min value");
>     return (x > T(0)) && ((x & (x - 1)) == T(0));
>   }
> 
> and it asserts in DaCapo in immL_Pow2 below:
> 
> 0x00007f33727e26ea in State::_sub_Op_ConL (this=0x7f3340842f60, 
> n=0x7f336cbb04b8) at src/hotspot/cpu/x86/x86_64.ad:3123
> 
> operand immL_Pow2()
> %{
>    predicate(is_power_of_2(n->get_long()));
>    match(ConL);
> 
>    op_cost(15);
>    format %{ %}
>    interface(CONST_INTER);
> %}
> 
> operand immL_NotPow2()
> %{
>    predicate(is_power_of_2(~n->get_long()));
>    match(ConL);
> 
>    op_cost(15);
>    format %{ %}
>    interface(CONST_INTER);
> %}
> 
> The predicate for Pow2 and NotPow2 will both be false because of this 
> change. Is that a problem?

Probably. I'll do some more digging.

-- 
Andrew Haley  (he/him)
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
https://keybase.io/andrewhaley
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671



More information about the hotspot-dev mailing list