Hi! In 4.8 and earlier we used to fold the following to 0 during GENERIC folding, but we don't do that anymore because ctor_for_folding etc. has been turned into a GIMPLE centric API, but as the testcase shows, it is invoked even during GENERIC folding and there the automatic vars still should have meaningful initializers. I've verified that the C++ FE drops TREE_READONLY on automatic vars with const qualified types if they require non-constant (runtime) initialization.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2021-01-26 Jakub Jelinek <ja...@redhat.com> PR tree-optimization/97260 * varpool.c (ctor_for_folding): In GENERIC return DECL_INITIAL for TREE_READONLY non-TREE_SIDE_EFFECTS automatic variables. * gcc.dg/tree-ssa/pr97260.c: New test. --- gcc/varpool.c.jj 2021-01-26 08:57:36.184290279 +0100 +++ gcc/varpool.c 2021-01-26 09:46:16.453619140 +0100 @@ -412,6 +412,12 @@ ctor_for_folding (tree decl) if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) { gcc_assert (!TREE_PUBLIC (decl)); + /* Unless this is called during FE folding. */ + if (!in_gimple_form + && TREE_READONLY (decl) + && !TREE_SIDE_EFFECTS (decl) + && DECL_INITIAL (decl)) + return DECL_INITIAL (decl); return error_mark_node; } --- gcc/testsuite/gcc.dg/tree-ssa/pr97260.c.jj 2021-01-26 09:45:42.933003954 +0100 +++ gcc/testsuite/gcc.dg/tree-ssa/pr97260.c 2021-01-26 09:45:17.277298476 +0100 @@ -0,0 +1,11 @@ +/* PR tree-optimization/97260 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump "return 0;" "optimized" } } */ + +int +foo (void) +{ + const char a[] = "1234"; + return __builtin_memcmp (a, "1234", 4); +} Jakub