[cfe-dev] Speculative load optimization

Alexey Zhikhartsev via cfe-dev cfe-dev at lists.llvm.org
Mon Apr 30 07:10:09 PDT 2018


It seems that this optimization should not be performed when strict
aliasing is disabled. However, GCC is being aggressive and performs
speculative loads on struct fields even with-fno-strict-aliasing.

Should LLVM perform this load speculation even with strict aliasing
disabled?

If no, what is the way to check for strict aliasing in the midend (with
strict aliasing being a frontend concept)? Is it reasonable to check for
presence of TBAA tags on the load instructions? And if there are any TBAA
tags associated with loads, consider strict aliasing enabled.
Best,
Alex


>
> -----Original Message-----
> From: cfe-dev [mailto:cfe-dev-bounces at lists.llvm.org] On Behalf Of
> Kreitzer, David L via cfe-dev
> Sent: Monday, October 09, 2017 5:17 PM
> To: cfe-dev at lists.llvm.org
> Subject: [cfe-dev] Speculative load optimization
>
> This llvm patch, https://reviews.llvm.org/D37289, attempts to do an
> optimization that involves speculating loads. The patch itself needs some
> work regardless, but we are questioning the legality of the optimization,
> which is currently performed by both gcc and icc. The desired
> transformation looks like this:
>
>     typedef struct S {
>       char padding[4088];
>       struct S *p1;
>       struct S *p2;
>     } S;
>
>     struct S* f1(struct S *s, int x)
>     {
>       S *r;
>       if (x)
>         r = s->p1;
>       else
>         r = s->p2;
>       return r;
>     }
>
> TO
>
>     struct S* f1(struct S *s, int x)
>     {
>       return (x) ? s->p1 : s->p2;
>     }
>
> The fundamental question seems to be whether loading one member of struct
> S makes it valid to load other members of the same struct. Both gcc & icc
> seem to think so and make a distinction between this case and a similar
> case where one field is accessed through an lvalue of a different type:
>
>     typedef struct T {
>       char padding[4088];
>       struct S *p1;
>     } T;
>
>     struct S* f2(struct S *s, int x)
>     {
>       S *r;
>       if (x)
>         r = ((T*)s)->p1;
>       else
>         r = s->p2;
>       return r;
>     }
>
> Neither compiler will transform this case.
>
> Any insight on the validity of this optimization in relation to the C/C++
> standards would be appreciated.
>
> Thanks,
> Dave Kreitzer
> Intel Compilers
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180430/e97b9ac7/attachment.html>


More information about the cfe-dev mailing list