public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/54021] New: [c++0x] __builtin_constant_p should be constexpr
@ 2012-07-18 19:52 luto at mit dot edu
  2012-07-19  6:53 ` [Bug c++/54021] " jakub at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: luto at mit dot edu @ 2012-07-18 19:52 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54021

             Bug #: 54021
           Summary: [c++0x] __builtin_constant_p should be constexpr
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: luto@mit.edu


It's hard to tell how __builtin_constant_p works right now, due to PR54020.

// Preliminaries.
extern int nonconst_func(int);
constexpr int identity(int x) { return x; }
constexpr int zero() { return identity(0); }
constexpr int one() { return identity(1); }

// These are the same.  Only the latter is accepted, though.
// I suspect that the acceptance of the latter is due to the bug above.
constexpr int rejected_const_4(int x) { return __builtin_constant_p(x) ? 4 :
nonconst_func(x); }
constexpr int accepted_const_4(int x) { return
identity(__builtin_constant_p(x)) ? 4 : nonconst_func(x); }

// This is rejected.  I would like it to work.
constexpr int four = accepted_const_4(1);

The ability to use the construction __builtin_constant_p(x) ? const_func(x) :
nonconst_func(x) in constexpr context would be quite powerful.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/54021] [c++0x] __builtin_constant_p should be constexpr
  2012-07-18 19:52 [Bug c++/54021] New: [c++0x] __builtin_constant_p should be constexpr luto at mit dot edu
@ 2012-07-19  6:53 ` jakub at gcc dot gnu.org
  2012-07-19  7:12 ` jakub at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2012-07-19  6:53 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54021

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org,
                   |                            |jason at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> 2012-07-19 06:53:11 UTC ---
This is rejected only at -O0 and quite matches how __builtin_constant_p
normally behaves at -O0 - you really need a constant right in the
__builtin_constant_p argument, everything else results in 0.  With -O1 and
above you get BUILTIN_CONSTANT_P not folded right away to 0 if not constant and
whether it in the end folds to 0 or 1 depends on optimization, whether a
constant is propagated to it or not.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/54021] [c++0x] __builtin_constant_p should be constexpr
  2012-07-18 19:52 [Bug c++/54021] New: [c++0x] __builtin_constant_p should be constexpr luto at mit dot edu
  2012-07-19  6:53 ` [Bug c++/54021] " jakub at gcc dot gnu.org
@ 2012-07-19  7:12 ` jakub at gcc dot gnu.org
  2012-07-19 20:02 ` jason at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2012-07-19  7:12 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54021

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> 2012-07-19 07:12:18 UTC ---
Perhaps the C++ FE could when parsing a constexpr function during
finish_call_expr of __builtin_constant_p just temporarily force optimize = 1
if it is zero to prevent folding it to 0 (or alternatively, if it folds to 0,
build it non-folded), it would be an extension over how this builtin behaves
right now, on the other side as constexpr is "optimized" even at -O0 it would
match the intent of the builtin.  The question is if/when it will be actually
folded to 0 afterwards if not in constexpr context.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/54021] [c++0x] __builtin_constant_p should be constexpr
  2012-07-18 19:52 [Bug c++/54021] New: [c++0x] __builtin_constant_p should be constexpr luto at mit dot edu
  2012-07-19  6:53 ` [Bug c++/54021] " jakub at gcc dot gnu.org
  2012-07-19  7:12 ` jakub at gcc dot gnu.org
@ 2012-07-19 20:02 ` jason at gcc dot gnu.org
  2012-07-19 20:57 ` jason at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jason at gcc dot gnu.org @ 2012-07-19 20:02 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54021

--- Comment #3 from Jason Merrill <jason at gcc dot gnu.org> 2012-07-19 20:02:13 UTC ---
Author: jason
Date: Thu Jul 19 20:02:08 2012
New Revision: 189677

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=189677
Log:
    PR c++/54021
    * call.c (build_cxx_call): Set optimize when folding
    __builtin_constant_p in a constexpr function.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-builtin2.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/call.c
    trunk/gcc/testsuite/ChangeLog


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/54021] [c++0x] __builtin_constant_p should be constexpr
  2012-07-18 19:52 [Bug c++/54021] New: [c++0x] __builtin_constant_p should be constexpr luto at mit dot edu
                   ` (2 preceding siblings ...)
  2012-07-19 20:02 ` jason at gcc dot gnu.org
@ 2012-07-19 20:57 ` jason at gcc dot gnu.org
  2012-09-08 21:36 ` david at doublewise dot net
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jason at gcc dot gnu.org @ 2012-07-19 20:57 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54021

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|---                         |4.8.0

--- Comment #4 from Jason Merrill <jason at gcc dot gnu.org> 2012-07-19 20:57:21 UTC ---
I'm not sure when exactly it gets folded later, but it does seem to happen
appropriately.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/54021] [c++0x] __builtin_constant_p should be constexpr
  2012-07-18 19:52 [Bug c++/54021] New: [c++0x] __builtin_constant_p should be constexpr luto at mit dot edu
                   ` (3 preceding siblings ...)
  2012-07-19 20:57 ` jason at gcc dot gnu.org
@ 2012-09-08 21:36 ` david at doublewise dot net
  2012-09-08 22:29 ` luto at mit dot edu
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: david at doublewise dot net @ 2012-09-08 21:36 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54021

David Stone <david at doublewise dot net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |david at doublewise dot net

--- Comment #5 from David Stone <david at doublewise dot net> 2012-09-08 21:36:10 UTC ---
I'm running into some issues with this bug, and it's much broader than the test
cases suggest. On gcc 4.7.0, this is what happens:

int main() {
    int x = 0;
    // This assigns false to a:
    bool const a = __builtin_constant_p(x);
    // This assigns true to b:
    bool const b = __builtin_constant_p(__builtin_constant_p(x));
    // This causes "error: the value of 'x' is not usable in a constant
expression"
    constexpr bool c = __builtin_constant_p(x);
}


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/54021] [c++0x] __builtin_constant_p should be constexpr
  2012-07-18 19:52 [Bug c++/54021] New: [c++0x] __builtin_constant_p should be constexpr luto at mit dot edu
                   ` (4 preceding siblings ...)
  2012-09-08 21:36 ` david at doublewise dot net
@ 2012-09-08 22:29 ` luto at mit dot edu
  2012-09-09  6:00 ` david at doublewise dot net
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: luto at mit dot edu @ 2012-09-08 22:29 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54021

--- Comment #6 from Andy Lutomirski <luto at mit dot edu> 2012-09-08 22:29:17 UTC ---
I think that's correct.  x isn't a standards-mandated constant expression, so
__builtin_constant_p depends on optimization level and probably shouldn't be
allowed as a constexpr.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/54021] [c++0x] __builtin_constant_p should be constexpr
  2012-07-18 19:52 [Bug c++/54021] New: [c++0x] __builtin_constant_p should be constexpr luto at mit dot edu
                   ` (5 preceding siblings ...)
  2012-09-08 22:29 ` luto at mit dot edu
@ 2012-09-09  6:00 ` david at doublewise dot net
  2012-09-09  6:05 ` luto at mit dot edu
  2012-09-09 16:48 ` jason at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: david at doublewise dot net @ 2012-09-09  6:00 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54021

--- Comment #7 from David Stone <david at doublewise dot net> 2012-09-09 06:00:37 UTC ---
That seems to me like saying that `constexpr bool d = sizeof(x);` should be
disallowed because it uses a non-constexpr. You're not using the value of x,
just a property about it. Whether a value is constexpr is guaranteed to be
known at compile time, and so should be usable as a constexpr.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/54021] [c++0x] __builtin_constant_p should be constexpr
  2012-07-18 19:52 [Bug c++/54021] New: [c++0x] __builtin_constant_p should be constexpr luto at mit dot edu
                   ` (6 preceding siblings ...)
  2012-09-09  6:00 ` david at doublewise dot net
@ 2012-09-09  6:05 ` luto at mit dot edu
  2012-09-09 16:48 ` jason at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: luto at mit dot edu @ 2012-09-09  6:05 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54021

--- Comment #8 from Andy Lutomirski <luto at mit dot edu> 2012-09-09 06:05:34 UTC ---
Did you mean "constexpr bool a" instead of "book const a"?  If so, I agree. 
But consider:

bool const a = something complicated

Is a a constant?


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/54021] [c++0x] __builtin_constant_p should be constexpr
  2012-07-18 19:52 [Bug c++/54021] New: [c++0x] __builtin_constant_p should be constexpr luto at mit dot edu
                   ` (7 preceding siblings ...)
  2012-09-09  6:05 ` luto at mit dot edu
@ 2012-09-09 16:48 ` jason at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: jason at gcc dot gnu.org @ 2012-09-09 16:48 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54021

--- Comment #9 from Jason Merrill <jason at gcc dot gnu.org> 2012-09-09 16:48:46 UTC ---
(In reply to comment #5)
>     // This causes "error: the value of 'x' is not usable in a constant expression"
>     constexpr bool c = __builtin_constant_p(x);
> }

This is also fixed by my patch.


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2012-09-09 16:48 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-18 19:52 [Bug c++/54021] New: [c++0x] __builtin_constant_p should be constexpr luto at mit dot edu
2012-07-19  6:53 ` [Bug c++/54021] " jakub at gcc dot gnu.org
2012-07-19  7:12 ` jakub at gcc dot gnu.org
2012-07-19 20:02 ` jason at gcc dot gnu.org
2012-07-19 20:57 ` jason at gcc dot gnu.org
2012-09-08 21:36 ` david at doublewise dot net
2012-09-08 22:29 ` luto at mit dot edu
2012-09-09  6:00 ` david at doublewise dot net
2012-09-09  6:05 ` luto at mit dot edu
2012-09-09 16:48 ` jason at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).