[BUG] GVN / Memdep bug in the presence of intrinsics

-- To avoid forgetting about this I post my bug here --

GVN, or probably memdep analysis, seem to think intrinsics are somewhat special
and consequently assume they cannot "effectively clobber" a memory location. It
might also be globals-mod-ref or something else that provides AA information.

The following example (https://godbolt.org/z/TG6dYvP6M) shows how an unknown function
prevents an invalid load hoist while an unknown intrinsic does not. Again, the preload
is not sound in a multi-thread environment.

opt -aa-pipeline=basic-aa,globals-aa -passes="require<globals-aa>,function(gvn<pre;load-pre;split-backedge-load-pre;memdep>)"

@G= internaladdrspace(3) globali32undef
declarevoid@llvm.sync()
definei32@sync_gvn_bug(i1%cond) nounwind{
entry:
br i1%cond, label%t, label%f
t:
store i3242, i32addrspace(3)* @G
br label%m
f:
br label%m
m:
call void@llvm.sync()
%r= load i32, i32addrspace(3)* @G
call void@llvm.sync()
ret i32%r
}
declarevoid@sync()
definei32@sync_gvn_bug_ok(i1%cond) nounwind{
entry:
br i1%cond, label%t, label%f
t:
store i3242, i32addrspace(3)* @G
br label%m
f:
br label%m
m:
call void@sync()
%r= load i32, i32addrspace(3)* @G
call void@sync()
ret i32%r
}

Help with fixing this is much appreciated!

~ Johannes

Hit this again, opened a bug with simple reproducer: GVN hoists loads over unknown intrinsics · Issue #54851 · llvm/llvm-project · GitHub