This patch optimizes the following case:
static int x; bool foo(int *n) { return &x == n; }
To remove the static global that isn't otherwise used:
bool foo(int *n) { return false; }
This is not a valid optimization in C. However, there are cases where
this can be valid in other language if the pointer parameter n is known
to point into a valid object (either via the dereferenceable_or_null
attribute or an inbouds gep). The logic here is the same as for
https://reviews.llvm.org/D60047.
The motivator for this is runtime type information in TinyGo. There are
two ways type information globals are referenced: either by storing it
in an interface value, or by comparing the interface type against the
type information global. It is this second use (the comparison) that
leads to a significant code size increase of double digit percents in
some cases.
We currently work around this using a hack, but I'd like this to be
supported natively in LLVM so we can simplify the compiler and make use
of a normal LTO pipeline instead of our homegrown LTO variant.
Why is this code in this file?