[PATCH] D148073: [SCEV] Improve willNotOverflow by checking min/max in wide range with context

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 12 00:46:21 PDT 2023


mkazantsev created this revision.
mkazantsev added reviewers: nikic, lebedev.ri, fhahn, skatkov, dmakogon, danilaml.
Herald added subscribers: StephenFan, zzheng, hiraditya.
Herald added a project: All.
mkazantsev requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This patch adds a context-aware analysis that tris to prove that `(Op, LHS, RHS)` does
not overflow. The basic version of this algorithm takes wide type which is 2x wider
than the original type and tries to prove that `ext(LHS + RHS) == ext(LHS) + ext(RHS)`,
and the context-aware analysis only works with constants and their ranges.

This one adds the following logic: we compute three values:

1. `B = ext(LHS) + ext(RHS)`;
2. `min = ext(MIN_VALUE)` (i.e. min value of LHS's original type);
3. `max = ext(MAX_VALUE)` (i.e. max value of LHS's original type);

And if we can prove that in the given context `min <= B <= max`, then the computation
was without overflow (i.e. result is still in bounds in terms of original type).

There is one regression with IV widening, but this transform has long been known
for being unstable and awkwardly broken, and it needs to be fixed separately.
In most cases it allows us to infer more no-wrap flags.


https://reviews.llvm.org/D148073

Files:
  llvm/lib/Analysis/ScalarEvolution.cpp
  llvm/test/Analysis/ScalarEvolution/guards.ll
  llvm/test/Analysis/ScalarEvolution/pr44605.ll
  llvm/test/Transforms/IndVarSimplify/X86/pr35406.ll
  llvm/test/Transforms/IndVarSimplify/canonicalize-cmp.ll
  llvm/test/Transforms/IndVarSimplify/elim-extend.ll
  llvm/test/Transforms/IndVarSimplify/lftr-reuse.ll
  llvm/test/Transforms/IndVarSimplify/predicated_ranges.ll
  llvm/test/Transforms/LoopUnroll/runtime-loop-multiple-exits.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148073.512677.patch
Type: text/x-patch
Size: 12348 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230412/e758850f/attachment.bin>


More information about the llvm-commits mailing list