Re: fix for strict-alias warnings - Mailing list pgsql-patches

From Manfred Spraul
Subject Re: fix for strict-alias warnings
Date
Msg-id 3F8C5D78.1090802@colorfullife.com
Whole thread Raw
In response to Re: fix for strict-alias warnings  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: fix for strict-alias warnings
List pgsql-patches
Tom Lane wrote:

>Given that gcc is smart enough not to move any code across the memset()
>call, I doubt that it would be moving anything across the whole if()
>construct.  Now if the if-condition were such that the memset code path
>could be optimized away, then we'd have a problem, but in practice I do
>not believe gcc is smart enough to realize that the alignment check is
>always true.
>
gcc-3.2.2 optimizes the memset away - that's a simple exercise for gcc.

gcc-3.2.2 isn't smart enough to replace everything - it didn't like the
pointer arithmetics.
After some massaging, I've succeeded in generating bad code using a
slightly modified MemSetAligned macro (parameters -O2
-fstrict-aliasing): gcc pipelined the x*x around the memset.

Annotated asm output with gcc -O99 -fomit-frame-pointer -fstrict-aliasing:
08048328 <test2>:
 8048328:       83 ec 18                sub    $0x18,%esp
 >>>>> stack setup for automatic variables.
 804832b:       c7 44 24 0c 00 00 00    movl   $0x0,0xc(%esp,1)
 8048332:       00
 8048333:       c7 44 24 10 00 00 00    movl   $0x40000000,0x10(%esp,1)
 804833a:       40
 >>>> x = 1.0;
 804833b:       dd 44 24 0c             fldl   0xc(%esp,1)
 804833f:       d8 c8                   fmul   %st(0),%st
 >>>> x = x*x;
 8048341:       c7 44 24 0c 00 00 00    movl   $0x0,0xc(%esp,1)
 8048348:       00
 8048349:       c7 44 24 10 00 00 00    movl   $0x0,0x10(%esp,1)
 8048350:       00
 >>>>> MemSetAligned(): optimized to storing two ints.
 8048351:       dd 54 24 0c             fstl   0xc(%esp,1)
 >>> write back the result of x*x to the stack
 8048355:       dd 1c 24                fstpl  (%esp,1)
 >>> push x*x for printf call
 8048358:       68 54 84 04 08          push   $0x8048454
 >>> push pointer to "square is %f.\n"
 804835d:       e8 06 ff ff ff          call   8048268 <_init+0x38>
 >>>> call printf
 8048362:       83 c4 1c                add    $0x1c,%esp
 8048365:       c3                      ret
 >>>>> and exit.
 8048366:       89 f6                   mov    %esi,%esi

To paraphrase the ISO C line: gcc is not your grandfather's gcc. It's
within 10% of the best compilers for SpecInt - the propagation and
analysis of constants it quite good, and several bugs were fixed sinced
3.2.2.

What is the oldest gcc versions still supported by postgres? It seems
that the strict alias analysis is from the egcs tree. Probably first
supported by egcs-1.1.2 - is that gcc-2.91?

http://groups.google.de/groups?q=g:thl2087564510d&dq=&hl=de&lr=&ie=UTF-8&oe=UTF-8&selm=fa.fjlldvv.l7m7hk%40ifi.uio.no

--
    Manfred
#include <stdio.h>
#include <stdlib.h>

typedef signed int int32;
typedef int Size;

#define INT_ALIGN_MASK    (sizeof(int)-1)
#define MEMSET_LOOP_LIMIT  1024

#define MemSetAligned(start, val, len) \
    do \
    { \
        int32 * _start = (int32 *) (start); \
        int        _val = (val); \
        Size    _len = (len); \
\
        if ((_len & INT_ALIGN_MASK) == 0 && \
            _val == 0 && \
            _len <= MEMSET_LOOP_LIMIT) \
        { \
            Size    offset; \
            _len = _len/sizeof(int); \
            for (offset=0;offset<_len;offset++) \
                _start[offset] = 0; \
        } \
        else \
            memset_is_this_function_optimized_away_question_mark((char *) _start, _val, _len); \
    } while (0)

void test2(void)
{
    double x;

    x = 2.0;

    MemSetAligned(&x, 0, sizeof(double));

    x = x*x;

    printf("square is %f.\n", x);
}

int main(void)
{
    test2();
    return 1;
}

pgsql-patches by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: fix for strict-alias warnings
Next
From: Neil Conway
Date:
Subject: Re: fix for strict-alias warnings