public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/51506] New: Function cloning misses constant struct
@ 2011-12-11 23:58 peteraward+gcc at gmail dot com
  2011-12-12 10:04 ` [Bug tree-optimization/51506] " rguenth at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: peteraward+gcc at gmail dot com @ 2011-12-11 23:58 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51506

             Bug #: 51506
           Summary: Function cloning misses constant struct
    Classification: Unclassified
           Product: gcc
           Version: 4.6.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: peteraward+gcc@gmail.com


The actual problem I’m dealing with is with avr-gcc, so the goal is to achieve
a small code size. I’m trying to write my code like this:
lcd_init(lcd_t l, ...)
where the first parameter is passed a *constant* struct which contains the
memory addresses of each of the pins for the LCD. Thus, I want the compiler to
note that all calls have the same first argument, clone the function, and
propagate the constant.

However, it doesn’t seem to be working in practice.
In trying to build this test case, I found the compiler would just inline all
the functions, which defeats the point (in the actual code, the cost of
inlining is too high). So, I’ve added the noinline attribute, which I don’t
think should stop this optimisation, but apologies if it does.

Anyhow, here’s the testcase.
(using gcc version 4.6.2 (Debian 4.6.2-5), on 64-bit Linux)

$ cat test.c
typedef struct {
    int a;
    int b;
} dint;

__attribute__((noinline))
static int compute_int(int x, int var) {
    int y = 0;
    for (int i = 0; i < x; i++)
        y += i * x;
    return y + var;
}

__attribute__((noinline))
static int compute_dint(dint x, int var) {
    int z = x.a + x.b;
    int y = 0;
    for (int i = 0; i < z; i++)
        y += i * z;
    return y + var;
}

int main() {
    int rv;
    rv += compute_dint((dint) {6, 1}, 1);
    rv += compute_dint((dint) {6, 1}, 2);
    rv += compute_dint((dint) {6, 1}, 3);
    rv += compute_int(5, 1);
    rv += compute_int(5, 2);
    rv += compute_int(5, 3);
    return rv;
}
$ gcc -fdump-ipa-all -fipa-cp -fipa-cp-clone -Os -std=c99 test.c

Expected result:
both compute_int and compute_dint should be optimised to versions where "x" is
constant.

Actual reslut:
only compute_int is optimised.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug tree-optimization/51506] Function cloning misses constant struct
  2011-12-11 23:58 [Bug tree-optimization/51506] New: Function cloning misses constant struct peteraward+gcc at gmail dot com
@ 2011-12-12 10:04 ` rguenth at gcc dot gnu.org
  2011-12-13  2:21 ` pinskia at gcc dot gnu.org
  2021-07-19  3:06 ` [Bug ipa/51506] Function cloning misses constant struct at -Os vs -O2 pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-12-12 10:04 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51506

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2011-12-12
                 CC|                            |jamborm at gcc dot gnu.org,
                   |                            |rguenth at gcc dot gnu.org
     Ever Confirmed|0                           |1
           Severity|normal                      |enhancement

--- Comment #1 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-12-12 10:02:22 UTC ---
We currently do not easily see that they are constant:

  D.1599.a = 6;
  D.1599.b = 1;
  D.1603_1 = compute_dint (D.1599, 1);

but we could in theory improve our IL by not forcing the aggregate argument
to a temporary during gimplification of

  rv = compute_dint (<<< Unknown tree: compound_literal_expr
    struct dint D.1599 = {.a=6, .b=1}; >>>, 1) + rv;

but simply allow !is_gimple_reg_type CONSTRUCTORs that are TREE_CONSTANT,
thus have

  D.1603_1 = compute_dint ({.a=6, .b=1}, 1);

in the IL.  That would still require ipa-cp to handle aggregates though.

The above would also mean that

  D.1599 = {.a=6, .b=1};

would be valid GIMPLE (I see no good reason to disallow this either).


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug tree-optimization/51506] Function cloning misses constant struct
  2011-12-11 23:58 [Bug tree-optimization/51506] New: Function cloning misses constant struct peteraward+gcc at gmail dot com
  2011-12-12 10:04 ` [Bug tree-optimization/51506] " rguenth at gcc dot gnu.org
@ 2011-12-13  2:21 ` pinskia at gcc dot gnu.org
  2021-07-19  3:06 ` [Bug ipa/51506] Function cloning misses constant struct at -Os vs -O2 pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2011-12-13  2:21 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51506

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> 2011-12-12 23:25:04 UTC ---
Hmm, shouldn't this be handled in steps?  First by IPA-SRA and then by IPA-CCP
?


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug ipa/51506] Function cloning misses constant struct at -Os vs -O2
  2011-12-11 23:58 [Bug tree-optimization/51506] New: Function cloning misses constant struct peteraward+gcc at gmail dot com
  2011-12-12 10:04 ` [Bug tree-optimization/51506] " rguenth at gcc dot gnu.org
  2011-12-13  2:21 ` pinskia at gcc dot gnu.org
@ 2021-07-19  3:06 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-19  3:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51506

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|tree-optimization           |ipa
            Summary|Function cloning misses     |Function cloning misses
                   |constant struct             |constant struct at -Os vs
                   |                            |-O2
                 CC|                            |marxin at gcc dot gnu.org
             Target|                            |avr

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So this is done at -O2 already but not -Os.  I have not looked into why though.
Someone will need to look at the heurstics to figure out why.

Also it just does not happen on avr even.  There must be some heurstic changes
between most other targets and avr that causes this not to happen.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-07-19  3:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-11 23:58 [Bug tree-optimization/51506] New: Function cloning misses constant struct peteraward+gcc at gmail dot com
2011-12-12 10:04 ` [Bug tree-optimization/51506] " rguenth at gcc dot gnu.org
2011-12-13  2:21 ` pinskia at gcc dot gnu.org
2021-07-19  3:06 ` [Bug ipa/51506] Function cloning misses constant struct at -Os vs -O2 pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).