[Bug c++/85746] Premature evaluation of __builtin_constant_p?

2019-10-22 Thread glisse at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85746

Marc Glisse  changed:

   What|Removed |Added

 Status|NEW |RESOLVED
 Resolution|--- |FIXED

--- Comment #10 from Marc Glisse  ---
Fixed in gcc-10.

[Bug c++/85746] Premature evaluation of __builtin_constant_p?

2019-10-22 Thread glisse at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85746

--- Comment #9 from Marc Glisse  ---
Author: glisse
Date: Tue Oct 22 14:42:38 2019
New Revision: 277292

URL: https://gcc.gnu.org/viewcvs?rev=277292=gcc=rev
Log:
PR c++/85746: Don't fold __builtin_constant_p prematurely

2019-10-22  Marc Glisse  

gcc/cp/
* constexpr.c (cxx_eval_builtin_function_call): Only set
force_folding_builtin_constant_p if manifestly_const_eval.

gcc/testsuite/
* g++.dg/pr85746.C: New file.

Added:
trunk/gcc/testsuite/g++.dg/pr85746.C
Modified:
trunk/gcc/cp/ChangeLog
trunk/gcc/cp/constexpr.c
trunk/gcc/testsuite/ChangeLog

[Bug c++/85746] Premature evaluation of __builtin_constant_p?

2019-07-08 Thread rsandifo at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85746

--- Comment #8 from rsandifo at gcc dot gnu.org  
---
(In reply to Marc Glisse from comment #7)
> (In reply to Marc Glisse from comment #6)
> >  && xi.val[0] <= (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT)
> >   HOST_WIDE_INT_MAX >> shift))
> 
> The issue occurs with xi.val[0] == -9223372036854775808 (lshift_large
> returns a result of length 2 for that). I don't know if the code mishandles
> this case, or if such a number is not supposed to exist in the first place,
> but that does seem like a bug.

Yeah, looks like this should have been an unsigned HOST_WIDE_INT comparison
instead, i.e. casting xi.val[0] rather than the shift result.

[Bug c++/85746] Premature evaluation of __builtin_constant_p?

2019-07-07 Thread glisse at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85746

Marc Glisse  changed:

   What|Removed |Added

 CC||rsandifo at gcc dot gnu.org

--- Comment #7 from Marc Glisse  ---
(In reply to Marc Glisse from comment #6)
>  && xi.val[0] <= (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT)
>   HOST_WIDE_INT_MAX >> shift))

The issue occurs with xi.val[0] == -9223372036854775808 (lshift_large returns a
result of length 2 for that). I don't know if the code mishandles this case, or
if such a number is not supposed to exist in the first place, but that does
seem like a bug.

[Bug c++/85746] Premature evaluation of __builtin_constant_p?

2019-07-07 Thread glisse at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85746

--- Comment #6 from Marc Glisse  ---
The problematic file seems to be tree-dfa.o, and the only difference I see in
the .original dump is the use of __builtin_constant_p in wi::lshift (3
occurrences because of templates)

-  if (precision <= 64)
+  if (__builtin_constant_p (xi.D.16865.D.16740.precision > 64) != 0 &&
xi.D.16865.D.16740.precision > 64 ? ((__builtin_constant_p (shift <= 62) != 0
&& shift <= 62) && xi.D.16865.D.16740.len == 1) && *NON_LVALUE_EXPR
 <= (const long int) (9223372036854775807 >> shift) :
precision <= 64)

which corresponds to the use of STATIC_CONSTANT_P in wide-int.h

  if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
  ? (STATIC_CONSTANT_P (shift < HOST_BITS_PER_WIDE_INT - 1)
 && xi.len == 1
 && xi.val[0] <= (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT)
  HOST_WIDE_INT_MAX >> shift))
  : precision <= HOST_BITS_PER_WIDE_INT)
{
  val[0] = xi.ulow () << shift;
  result.set_len (1);
}

[Bug c++/85746] Premature evaluation of __builtin_constant_p?

2019-07-06 Thread glisse at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85746

--- Comment #5 from Marc Glisse  ---
The patch breaks 3 tests on a bootstrapped compiler:
c-c++-common/Warray-bounds-2.c
c-c++-common/ubsan/ptr-overflow-sanitization-1.c
gcc.dg/Warray-bounds-30.c

The last one in particular shows that the compiler is miscompiled (it works
with the stage 1 compiler but breaks with stage 2 or 3). From the dumps, the
compilation seems to diverge in FRE1, so the miscompiled function might be in a
function called from FRE.
I find it suspicious that just refusing to fold __builtin_constant_p can cause
this. The 2 explanations I can think of are:
- latent bug uncovered by the change
- a variable that needs to be statically initialized (order of initialization?)
and ends up dynamically initialized because of the change

[Bug c++/85746] Premature evaluation of __builtin_constant_p?

2019-07-06 Thread glisse at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85746

--- Comment #4 from Marc Glisse  ---
Created attachment 46566
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=46566=edit
untested patch to fold _bcp only when necessary

[Bug c++/85746] Premature evaluation of __builtin_constant_p?

2018-05-11 Thread glisse at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85746

--- Comment #3 from Marc Glisse  ---
(In reply to Jakub Jelinek from comment #2)
> For different versions there is the
> http://gcc.gnu.org/ml/gcc-patches/2018-03/msg00355.html
> patch.

Time to ping that one? ;-)
(I don't have a particular opinion about how __builtin_early_constant_p should
behave)

[Bug c++/85746] Premature evaluation of __builtin_constant_p?

2018-05-11 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85746

Jakub Jelinek  changed:

   What|Removed |Added

 CC||jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek  ---
For different versions there is the
http://gcc.gnu.org/ml/gcc-patches/2018-03/msg00355.html
patch.

[Bug c++/85746] Premature evaluation of __builtin_constant_p?

2018-05-11 Thread rguenth at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85746

Richard Biener  changed:

   What|Removed |Added

   Keywords||missed-optimization
 Status|UNCONFIRMED |NEW
   Last reconfirmed||2018-05-11
 Ever confirmed|0   |1

--- Comment #1 from Richard Biener  ---
Confirmed.