[BasicAA] Fix nsw handling for negated scales (PR63266)
authorNikita Popov <npopov@redhat.com>
Mon, 19 Jun 2023 12:21:40 +0000 (14:21 +0200)
committerNikita Popov <npopov@redhat.com>
Tue, 27 Jun 2023 07:40:09 +0000 (09:40 +0200)
commitc31eb827b72da784d7f18512a51e5396ac302d72
tree327ae99d96b13564ce66d285a124fbf995ce9f9e
parent51b0398b76fae2b85b9eec440c7ed63908bf30f3
[BasicAA] Fix nsw handling for negated scales (PR63266)

We currently preserve the nsw flag when negating scales, which is
incorrect for INT_MIN.

However, just dropping the NSW flag in this case makes BasicAA
behavior unreliable and asymmetric, because we may or may not
drop the NSW flag depending on which side gets subtracted.

Instead, leave the Scale alone and add an additional IsNegated flag,
which indicates that the whole VarIndex should be interpreted as a
subtraction. This allows us to retain the NSW flag.

When accumulating the offset range, we need to use subtraction
instead of adding for IsNegated indices. Everything else works on
the absolute value of the scale, so the negation does not matter
there.

Fixes https://github.com/llvm/llvm-project/issues/63266.

Differential Revision: https://reviews.llvm.org/D153270
llvm/lib/Analysis/BasicAliasAnalysis.cpp
llvm/test/Analysis/BasicAA/range.ll