[BasicAA] Generalize base offset modulus handling
authorNikita Popov <nikita.ppv@gmail.com>
Sun, 8 Nov 2020 08:48:38 +0000 (09:48 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Wed, 18 Nov 2020 20:48:49 +0000 (21:48 +0100)
commitcd3c22c47e4bef347599e137ee6b1c3c510d7902
treeadd674c1d144f1facb383a4770f939dbf10f255a
parentca76e9f34da844d5f78029f0d437efef08b4dd3f
[BasicAA] Generalize base offset modulus handling

The GEP aliasing implementation currently has two pieces of code
that solve two different subsets of the same basic problem: If you
have GEPs with offsets 4*x + 0 and 4*y + 1 (assuming access size 1),
then they do not alias regardless of whether x and y are the same.

One implementation is in aliasSameBasePointerGEPs(), which looks at
this in a limited structural way. It requires both GEP base pointers
to be exactly the same, then (optionally) a number of equal indexes,
then an unknown index, then a non-equal index into a struct. This
set of limitations works, but it's overly restrictive and hides the
core property we're trying to exploit.

The second implementation is part of aliasGEP() itself and tries to
find a common modulus in the scales, so it can then check that the
constant offset doesn't overlap under modular arithmetic. The second
implementation has the right idea of what the general problem is,
but effectively only considers power of two factors in the scales
(while aliasSameBasePointerGEPs also works with non-pow2 struct sizes.)

What this patch does is to adjust the aliasGEP() implementation to
instead find the largest common factor in all the scales (i.e. the GCD)
and use that as the modulus.

Differential Revision: https://reviews.llvm.org/D91027
llvm/lib/Analysis/BasicAliasAnalysis.cpp
llvm/test/Analysis/BasicAA/struct-geps.ll