[BasicAA] Model implicit trunc of GEP indices
authorNikita Popov <nikita.ppv@gmail.com>
Wed, 29 Sep 2021 20:18:54 +0000 (22:18 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Fri, 22 Oct 2021 21:47:02 +0000 (23:47 +0200)
commit61cfdf636dc720c0af9d1ba42ead5aea8401d34b
tree9e7c404f1ef03c262b557626cb28315f41ff6a03
parenta5a493e1920572ca78b77fcdaef68c26e96d25e7
[BasicAA] Model implicit trunc of GEP indices

GEP indices larger than the GEP index size are implicitly truncated
to the index size. BasicAA currently doesn't model this, resulting
in incorrect alias analysis results.

Fix this by explicitly modelling truncation in CastedValue in the
same way we do zext and sext. Additionally we need to disable a
number of optimizations for truncated values, in particular
"non-zero" and "non-equal" may no longer hold after truncation.
I believe the constant offset heuristic is also not necessarily
correct for truncated values, but wasn't able to come up with a
test for that one.

A possible followup here would be to use the new mechanism to
model explicit trunc as well (which should be much more common,
as it is the canonical form). This is straightforward, but omitted
here to separate the correctness fix from the analysis improvement.

(Side note: While I say "index size" above, BasicAA currently uses
the pointer size instead. Something for another day...)

Differential Revision: https://reviews.llvm.org/D110977
llvm/lib/Analysis/BasicAliasAnalysis.cpp
llvm/test/Analysis/BasicAA/gep-implicit-trunc-32-bit-pointers.ll