Summary: | __builtin_constant_p cannot resolve to const when optimizing | ||
---|---|---|---|
Product: | gcc | Reporter: | Erich Plondke <eplondke> |
Component: | c | Assignee: | Not yet assigned to anyone <unassigned> |
Status: | NEW --- | ||
Severity: | normal | CC: | desrt, eplondke, gcc-bugs, ian, jakub, jbeulich, jsm28, prlw1, przemoc |
Priority: | P2 | Keywords: | rejects-valid |
Version: | 3.4.2 | ||
Target Milestone: | --- | ||
Host: | Target: | ||
Build: | Known to work: | ||
Known to fail: | Last reconfirmed: | 2005-12-18 01:38:34 | |
Attachments: |
Test case as a file.
gcc49-pr19449.patch |
Description
Erich Plondke
2005-01-14 19:11:20 UTC
Created attachment 7961 [details]
Test case as a file.
Test case as a file.
The problem is the __builtin_constant_p is delayed for evalutation until optimizations are run but __builtin_choose_expr needs an answer right away. What we could do is for when processing the first argument of __builtin_choose_expr, say we are not in a function. *** Bug 46711 has been marked as a duplicate of this bug. *** *** Bug 56759 has been marked as a duplicate of this bug. *** Created attachment 29742 [details] gcc49-pr19449.patch Untested patch. There is another case where we'd better fold __builtin_constant_p right away, for static/extern function-local array dimensions: int y; static char a[__builtin_constant_p (y) ? -1 : 1]; extern char b[__builtin_constant_p (y) ? -1 : 1]; char d[__builtin_constant_p (y) ? -1 : 1]; void foo (int x) { static char e[__builtin_constant_p (x) ? -1 : 1]; extern char f[__builtin_constant_p (x) ? -1 : 1]; auto char g[__builtin_constant_p (x) ? -1 : 1]; char h[__builtin_constant_p (x) ? -1 : 1]; } Right now this compiles fine for -O0, but for -O1 and above it errors on e and f (twice on the latter actually). When cfun == NULL, we always fold __builtin_constant_p right away, but when cfun is NULL, we don't consider static/extern. Not sure how to fix this issue though, because I think the declspecs aren't passed down to declarator parsing. Author: jakub Date: Wed Apr 3 09:17:44 2013 New Revision: 197393 URL: http://gcc.gnu.org/viewcvs?rev=197393&root=gcc&view=rev Log: PR c/19449 * tree.h (force_folding_builtin_constant_p): New decl. * builtins.c (force_folding_builtin_constant_p): New variable. (fold_builtin_constant_p): Fold immediately also if force_folding_builtin_constant_p. * c-parser.c (c_parser_get_builtin_args): Add choose_expr_p argument. If set, or it temporarily for parsing of the first argument into force_folding_builtin_constant_p. (c_parser_postfix_expression): Adjust callers. * gcc.c-torture/execute/pr19449.c: New test. Added: trunk/gcc/testsuite/gcc.c-torture/execute/pr19449.c Modified: trunk/gcc/ChangeLog trunk/gcc/builtins.c trunk/gcc/c/ChangeLog trunk/gcc/c/c-parser.c trunk/gcc/testsuite/ChangeLog trunk/gcc/tree.h Another case, and this one fails at all optimisation levels: int a(void) { return 0; } /* This always returns a constant expression. If we can't statically * determine its value, then this is the constant expression '0'. */ #define CONSTIFY(x) (__builtin_constant_p(x)&&(x)) /* works fine */ int b = CONSTIFY(a()); int c = CONSTIFY(a()) ? 2 : 3; /* fails */ int d = __builtin_choose_expr(CONSTIFY(a()), 2, 3); |