EPICS Controls Argonne National Laboratory

Experimental Physics and
Industrial Control System

2002  2003  2004  2005  <20062007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  2024  Index 2002  2003  2004  2005  <20062007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  2024 
<== Date ==> <== Thread ==>

Subject: Re: C strict aliasing rules
From: Eric Norum <[email protected]>
To: Andrew Johnson <[email protected]>
Cc: Core talk list <[email protected]>
Date: Mon, 27 Nov 2006 16:43:29 -0600
On Nov 27, 2006, at 12:23 PM, Andrew Johnson wrote:

Eric Norum wrote:
static inline void
writethree(int *p)
{
    short *palias = (short *)p;
   *palias = 3;
}
int
testalias()
{
    int value = 12345678;
    writethree(&value);
    return value;
}
One might expect that the value returned from testalias() would be 12345678 with either its most-significant 16 bits or least- significant 16 bits set to 3 (on most architectures).
....
One would be wrong, though.

I don't think life is quite as bad as it might seem; if you take off the "static inline" keywords from writethree(), I suspect the optimizer may not be able to be quite as aggressive in throwing out code - please experiment and respond to that suggestion.

In that case the optimizer does not remove the function, but it does remove the call to it and its effect from testalias:
norume@gnarly 679> cat a.c
void
writethree(int *p)
{
    short *palias = (short *)p;
   *palias = 3;
}

int
testalias()
{
    int value = 12345678;
    writethree(&value);
    return value;
}
norume@gnarly 680> m68k-rtems4.7-gcc -O4 -Wall -S a.c
norume@gnarly 681> cat a.s
#NO_APP
        .file   "a.c"
        .text
        .align  2
        .globl  writethree
        .type   writethree, @function
writethree:
        link.w %fp,#0
        move.l 8(%fp),%a0
        move.w #3,(%a0)
        unlk %fp
        rts
        .size   writethree, .-writethree
        .align  2
        .globl  testalias
        .type   testalias, @function
testalias:
        link.w %fp,#-4
        move.l #12345678,%d0
        unlk %fp
        rts
        .size   testalias, .-testalias
        .ident  "GCC: (GNU) 4.1.1"


=====================================================================
The writethree function doesn't even have to come first. The optimizer appears to work on the file after it's all been consumed:
norume@gnarly 686> cat a.c
int
testalias()
{
    int value = 12345678;
    writethree(&value);
    return value;
}
void
writethree(int *p)
{
    short *palias = (short *)p;
   *palias = 3;
}

norume@gnarly 687> !m6
m68k-rtems4.7-gcc -O4 -Wall -S a.c
a.c: In function 'testalias':
a.c:5: warning: implicit declaration of function 'writethree'
a.c: At top level:
a.c:10: warning: conflicting types for 'writethree'
a.c:5: warning: previous implicit declaration of 'writethree' was here
norume@gnarly 688> !ca
cat a.c
int
testalias()
{
    int value = 12345678;
    writethree(&value);
    return value;
}
void
writethree(int *p)
{
    short *palias = (short *)p;
   *palias = 3;
}


Since we don't (shouldn't?) have any C routines in base that are marked as "static inline", I don't think our problems are as large as you make out them to be. I suspect the rules for C++ are subtly different, so it would also be interesting to see what using the C+ + compiler generates.

I'm afraid that the above test shows that any code is a candidate for removal by the optimizer. :-( I think that we should give consideration to adding -fno-strict- alaising to all gcc invocations and the equivalent to any other compilers we can find.



--
Eric Norum <[email protected]>
Advanced Photon Source
Argonne National Laboratory
(630) 252-4793



Replies:
Re: C strict aliasing rules Benjamin Franksen
References:
C strict aliasing rules Eric Norum
Re: C strict aliasing rules Andrew Johnson

Navigate by Date:
Prev: Re: C strict aliasing rules Andrew Johnson
Next: Re: C strict aliasing rules Benjamin Franksen
Index: 2002  2003  2004  2005  <20062007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  2024 
Navigate by Thread:
Prev: Re: C strict aliasing rules Andrew Johnson
Next: Re: C strict aliasing rules Benjamin Franksen
Index: 2002  2003  2004  2005  <20062007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  2024 
ANJ, 02 Feb 2012 Valid HTML 4.01! · Home · News · About · Base · Modules · Extensions · Distributions · Download ·
· Search · EPICS V4 · IRMIS · Talk · Bugs · Documents · Links · Licensing ·