Bug 45410 - constant not optimized / propagated
Summary: constant not optimized / propagated
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.4.3
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2010-08-26 05:13 UTC by Reza Roboubi
Modified: 2012-02-02 17:28 UTC (History)
2 users (show)

See Also:
Host: x86 Ubuntu Linux
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-02-02 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Reza Roboubi 2010-08-26 05:13:13 UTC
gcc -Wall -O3 -S test.c
doesn't seem to propagate constants correctly.

Notice that ss is declared static, therefor everything is known at compile time.

Code:
#include <stdio.h>

struct s {int i;int j;};
struct s static ss={77,2};

int
main() 
{
        ss.j += 88;
        printf("%d\n",sizeof(ss));
        return ss.i+ss.j;
};
Comment 1 Andrew Pinski 2010-08-26 05:19:02 UTC
Well GCC does not know much about main really.  It does not currently take into account that ss cannot change before main.
Comment 2 Jakub Jelinek 2010-08-26 07:33:57 UTC
That's not the main problem here.  The problem is that this is an aggregate and we SRA only automatic vars, not global vars.  And it isn't constant either (neither declared, nor actually, so can't be detected as constant).  Special handling of main would just add info that it is called only once in the lifetime of the program.
Comment 3 Richard Biener 2010-08-26 09:46:12 UTC
Well, you need flow-sensitive analysis to figure out that when visiting
ss.j += 88; you can assume that ss.j is still 2.
Comment 4 Jakub Jelinek 2010-08-26 09:59:05 UTC
Sure, one that would also be able to assume that main is called just once.
Anyway, even to optimize ss.i into 77 you'd need a way to mark fields in global aggregates as const if they aren't ever modified...
Comment 5 Reza Roboubi 2010-08-27 17:59:39 UTC
(In reply to comment #2)
> That's not the main problem here.  The problem is that this is an aggregate and
> we SRA only automatic vars, not global vars.  And it isn't constant either

Jakub,
Thanks for that insight.  Of course, gcc does a pretty good job (and flow analysis) if we move 'struct s ss' into function scope.  That seems like a reasonable workaround for many situations.

Reza.
Comment 6 Andrew Pinski 2012-02-02 17:28:36 UTC
Confirmed.