[PATCH] D47767: [SCEV] Drop cached info after late nsw/nuw flags setting

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 5 04:34:29 PDT 2018


mkazantsev created this revision.
mkazantsev added reviewers: sanjoy, skatkov, apilipenko, reames, DaniilSuchkov.
Herald added a subscriber: javed.absar.

SCEV sometimes set nuw/nsw flags to AddRecs lately. As a result, the cached data
about this AddRec may be too pessimistic. For example, we keep seeing the following
pattern over and over again:

1. An AddRec `{0,+,1}` is created without any flags, and its range is set to full-set;
2. Later, from loop iteration count, we derive nsw for this SCEV and set it;
3. Despite that, we are unable to prove non-negativity of such SCEV basing on cached range.

The complete and comprehensive fix for this problem will be to get rid of setNoWrapsFlag at all.
We should be able to derive them in the moment of SCEV creation. However, from what I see in
the comments, this may lead to infinite recursion: we need loop iteration count to create SCEVAddRec
and we need SCEVAddRec to compute this count. We need to study thoroughly if it is possible to move
to this new model without introducing this infinite logic.

So now we propose an alternative solution which is not comprehensive, but should be good enough
in most real cases. After late setting of nsw/nuw flags, we drop all cached data related to this SCEV,
in particular ranges data, and give SCEV a chance to derive more optimistic estimations basing on the flags.

The practical outcome of that solution is that we can handle a typical case in IV widening, when
SCEV fails to prove non-negativity of the IV (even after it derived respective no-wrap flags) and cannot
widen latch condition. As result, we have `trunc` in loop and latch condition of narrow type, while we
could have it widened as well.


https://reviews.llvm.org/D47767

Files:
  lib/Analysis/ScalarEvolution.cpp
  test/Transforms/IndVarSimplify/full_widening.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47767.149934.patch
Type: text/x-patch
Size: 7231 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180605/1f6e0af4/attachment.bin>


More information about the llvm-commits mailing list