Bug 94010 - [missed optimization] constant automatic string variable not elided
Summary: [missed optimization] constant automatic string variable not elided
Status: RESOLVED DUPLICATE of bug 59863
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 9.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2020-03-03 04:34 UTC by Aleksey Covacevice
Modified: 2020-03-04 07:37 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-03-03 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Aleksey Covacevice 2020-03-03 04:34:22 UTC
-fmerge-all-constants misses a very simple optimization scenario involving an array:

    extern void g(const char* c);

    void f() {
        const char c[] = "12345";
        g(c);
    }

produces a local array (with or without the flag).

If `const char c[]` is replaced by `const char* c`, the above code produces a global array as expected.

This seems to be the case in every GCC version I have tested, starting from 4.9.
Comment 1 Richard Biener 2020-03-03 08:33:09 UTC
It isn't so much about -fmerge-all-constants, this is about not eliding
the automatic variable for the string constant.

Note it may be the language standard doesn't permit changing the
linkage of 'c', code may end up writing to it, expecting automatic
storage.

So this eliding could be done by the frontend already.  Note there's heuristic
in the gimplifier to eventually emit a copy from a string constant rather
than doing inline initialization but the gimplifier cannot really elide the
variable.

Also this must have come up in the past.
Comment 2 Jakub Jelinek 2020-03-03 10:36:47 UTC
Just use static if you want to mege the constant...
Comment 3 Aleksey Covacevice 2020-03-03 19:26:50 UTC
(In reply to Richard Biener from comment #1)
> It isn't so much about -fmerge-all-constants, this is about not eliding
> the automatic variable for the string constant.
> 
> Note it may be the language standard doesn't permit changing the
> linkage of 'c', code may end up writing to it, expecting automatic
> storage.
> 

Richard, the same optimization is performed by Clang but it requires -fmerge-all-constants (GCC indeed doesn't, in the pointer case).

So, perhaps, this optimization should be still related to this flag since the language standard might not allow for such (and the flag breaks compliance anyway)?

> So this eliding could be done by the frontend already.  Note there's
> heuristic
> in the gimplifier to eventually emit a copy from a string constant rather
> than doing inline initialization but the gimplifier cannot really elide the
> variable.
> 
> Also this must have come up in the past.
Comment 4 jsm-csl@polyomino.org.uk 2020-03-03 22:16:45 UTC
See bug 59863, with discussion of the requirement for distinct copies of 
the array in recursive calls to have distinct addresses.  (Thus, this 
would definitely not be valid without -fmerge-all-constants, as the 
example in this bug could involve a recursive call - whereas the example 
in bug 59863 does not involve the address of the const array escaping, so 
the transformation would be valid in that example even without 
-fmerge-all-constants.  It's not valid for code to write to an array 
defined as const, however.)
Comment 5 Richard Biener 2020-03-04 07:37:26 UTC
duplicate then.

*** This bug has been marked as a duplicate of bug 59863 ***