[InstCombine] Allow memcpys from constant memory to readonly nocapture parameters...
authorPatrick Walton <pcwalton@fb.com>
Thu, 27 Oct 2022 02:54:26 +0000 (19:54 -0700)
committerPatrick Walton <pcwalton@fb.com>
Sun, 30 Oct 2022 21:41:03 +0000 (14:41 -0700)
commit36bbd68667dbe46626982d3a150891fb85bcc9a8
tree06b10973bdff8d80da9d89219652a864d0c719ca
parentbf738d2e77846826964402f2cccdd0681c71c038
[InstCombine] Allow memcpys from constant memory to readonly nocapture parameters to be elided.

Currently, InstCombine can elide a memcpy from a constant to a local alloca if
that alloca is passed as a nocapture parameter to a *function* that's readnone
or readonly, but it can't forward the memcpy if the *argument* is marked
readonly nocapture, even though readonly guarantees that the callee won't
mutate the pointee through that pointer. This patch adds support for detecting
and handling such situations, which arise relatively frequently in Rust, a
frontend that liberally emits readonly.

A more general version of this optimization would use alias analysis to check
the call's ModRef info for the pointee, but I was concerned about blowing up
compile time, so for now I'm just checking for one of readnone on the function,
readonly on the function, or readonly on the parameter.

Differential Revision: https://reviews.llvm.org/D136822
llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
llvm/test/Transforms/InstCombine/memcpy-from-global.ll