This is an archive of the discontinued LLVM Phabricator instance.

[SCEV] Improve willNotOverflow by checking min/max in wide range with context
AbandonedPublic

Authored by mkazantsev on Apr 12 2023, 12:46 AM.

Details

Summary

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.

Diff Detail

Event Timeline

mkazantsev created this revision.Apr 12 2023, 12:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 12 2023, 12:46 AM
mkazantsev requested review of this revision.Apr 12 2023, 12:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 12 2023, 12:46 AM
mkazantsev added inline comments.Apr 12 2023, 12:47 AM
llvm/test/Analysis/ScalarEvolution/guards.ll
26

Arguably regression

llvm/test/Transforms/IndVarSimplify/elim-extend.ll
141

Regression, but apparently fix should be in other place.

Can we do it under expensive sharpening flag? It's really annoying how CT constrains cuts capabilities of SCEV to use contextual facts...

Found a regression rebased on it.

mkazantsev planned changes to this revision.Apr 17 2023, 11:22 PM
mkazantsev added inline comments.
llvm/test/Transforms/IndVarSimplify/strengthen-overflow.ll
307

Regression.

mkazantsev added inline comments.Apr 18 2023, 12:17 AM
llvm/test/Transforms/IndVarSimplify/strengthen-overflow.ll
307

This regression isn't even because the analysis gives a better result. It's simply because we make a query... Weird.

mkazantsev abandoned this revision.Apr 18 2023, 2:30 AM

I'm going with more lightweight solution.