[GVN] Adjust metadata for coerced load CSE
authorNikita Popov <npopov@redhat.com>
Fri, 14 Apr 2023 09:48:54 +0000 (11:48 +0200)
committerNikita Popov <npopov@redhat.com>
Mon, 17 Apr 2023 10:52:31 +0000 (12:52 +0200)
commit8cdca966902a5af9c28b5f6fb8602b22a07808a1
treef9522ae38b152fb366bf6288c8e9a23ab28b269f
parent69ee6533131d030bc724193bfc1b91fcc99a81d2
[GVN] Adjust metadata for coerced load CSE

When reusing a load in a way that requires coercion (i.e. casts or
bit extraction) we currently fail to adjust metadata. Unfortunately,
none of our existing tooling for this is really suitable, because
combineMetadataForCSE() expects both loads to have the same type.
In this case we may work on loads of different types and possibly
offset memory location.

As such, what this patch does is to simply drop all metadata, with
the following exceptions:

* Metadata for which violation is known to always cause UB.
* If the load is !noundef, keep all metadata, as this will turn
  poison-generating metadata into UB as well.

This fixes the miscompile that was exposed by D146629.

Differential Revision: https://reviews.llvm.org/D148129
llvm/lib/Transforms/Scalar/GVN.cpp
llvm/test/Transforms/GVN/metadata.ll