[SCEV] Fix buggy behavior in getAddExpr with truncs
authorMax Kazantsev <max.kazantsev@azul.com>
Thu, 19 Jul 2018 01:46:21 +0000 (01:46 +0000)
committerMax Kazantsev <max.kazantsev@azul.com>
Thu, 19 Jul 2018 01:46:21 +0000 (01:46 +0000)
commitd41faecc4969a3c707f98a9925f6c88e895f9bde
treecebcafe57c0f57e8f9a300c746dbdb9be6fc0222
parent6b87e0c18f049172b33144f561cf4baa8b225d8d
[SCEV] Fix buggy behavior in getAddExpr with truncs

SCEV tries to constant-fold arguments of trunc operands in SCEVAddExpr, and when it does
that, it passes wrong flags into the recursion. It is only valid to pass flags that are proved for
narrow type into a computation in wider type if we can prove that trunc instruction doesn't
actually change the value. If it did lose some meaningful bits, we may end up proving wrong
no-wrap flags for sum of arguments of trunc.

In the provided test we end up with `nuw` where it shouldn't be because of this bug.

The solution is to conservatively pass `SCEV::FlagAnyWrap` which is always a valid thing to do.

Reviewed By: sanjoy
Differential Revision: https://reviews.llvm.org/D49471

llvm-svn: 337435
llvm/lib/Analysis/ScalarEvolution.cpp
llvm/test/Analysis/ScalarEvolution/truncate.ll