https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57359
--- Comment #23 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #22)
> Created attachment 48311 [details]
> patch
>
> Note that apart from the possible bad impact on optimization when fixing this
> bug an actual fix is complicated by the custom "optimized" dependence
> analysis
> code in the loop invariant motion pass.
>
> A conservative "simple" patch would be the attached but that doesn't preserve
> store-motion for the following (because the LIM data dependence code doesn't
> care about stmt order):
>
> typedef int A;
> typedef float B;
>
> void __attribute__((noinline,noclone))
> foo(A *p, B *q, long unk)
> {
> for (long i = 0; i < unk; ++i) {
> q[i] = 42;
> *p = 1;
> }
> }
>
> usually this bug doesn't manifest itself but of course the fix will be
> experienced everywhere. Benchmarking the simple patch might reveal
> it's not an issue (but I doubt that...).
Which means a better approach might be to, in addition to the existing
dependence testing, verify we can sink the stores through all exits
(IIRC there is/was a similar bug involving ordering of stores sunk where we'd
have to preserve the order). There's again the difficult cases like
for ()
{
*p = 1;
q[i] = 42;
if (test)
*p = 2;
}
which we currently sink as
for ()
{
p_1 = 1;
q[i] = 42;
if (test)
p_1 = 2;
}
*p = p_1;
if we want to preserve all sinking we'd have to replay all [possibly
aliased] stores - again more difficult when the store we sink is
in the latch (or when multiple exits are involved). For the above
example do
for ()
{
p_1 = 1;
q[i] = 42;
if (test)
p_1 = 2;
}
*p = p_1;
q[i] = 42;
if (test)
*p = p_1;
with scanning for valid re-orderings starting from all exits we want
to consider sinking two stores that cannot be reordered.
Or we want to re-think the whole store-motion data dependence code.