[Bug middle-end/92419] const variable aliases lead to folding non-const initializers
rguenth at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Fri Nov 8 10:49:00 GMT 2019
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92419
--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
bool
varpool_node::ctor_useable_for_folding_p (void)
{
...
/* Alias of readonly variable is also readonly, since the variable is stored
in readonly memory. We also accept readonly aliases of non-readonly
locations assuming that user knows what he is asking for. */
if (!TREE_READONLY (decl) && !TREE_READONLY (real_node->decl))
return false;
so looks like this is done on purpose? Shouldn't we then diagnose
those aliases? I'd have done
if (!TREE_READONLY (real_node->decl))
return false;
since as the comment says whether the variable may be stored in readonly
memory or not is what matters. Was there a real reason to be this
forgiving for the testcase at hand? Just to avoid the "asymmetry"
with the non-const alias of a const var?
Better testcase w/o unused 'tem':
int var = 3;
extern const int const_var __attribute__((alias("var")));
void bar()
{
var = 5;
}
int foo()
{
int tem = const_var;
bar ();
return const_var - tem;
}
int main()
{
if (foo() != 2)
__builtin_abort ();
return 0;
}
More information about the Gcc-bugs
mailing list