This is an archive of the discontinued LLVM Phabricator instance.

[Analysis] allow caller to choose signed/unsigned when computing constant range
ClosedPublic

Authored by spatel on Dec 27 2021, 1:36 PM.

Details

Summary

We should not lose analysis precision if an 'add' has both no-wrap flags (nsw and nuw) compared to just one or the other.

This patch is modeled on a similar construct that was added with D59386.

I don't think it is possible to expose a problem with an unsigned compare because of the way this was coded (nuw is handled first).

InstCombine has an assert that fires with the example from:
https://github.com/llvm/llvm-project/issues/52884
...because it was expecting InstSimplify to handle this kind of pattern with an smax.

Diff Detail

Event Timeline

spatel created this revision.Dec 27 2021, 1:36 PM
spatel requested review of this revision.Dec 27 2021, 1:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 27 2021, 1:36 PM
nikic accepted this revision.Dec 28 2021, 5:33 AM

LGTM. I implemented something similar in https://reviews.llvm.org/D59668 and then dropped it because my implementation was broken and my immediate need went away :)

This revision is now accepted and ready to land.Dec 28 2021, 5:33 AM

LGTM. I implemented something similar in https://reviews.llvm.org/D59668 and then dropped it because my implementation was broken and my immediate need went away :)

Thanks - I didn't remember that patch, but yes, it's very close to this.

Our exhaustive unit testing has matured since then, so it shouldn't be too hard to implement. I'll look at that as a follow-up. I'd like to get this in to avoid the crashing bug.

This revision was landed with ongoing or failed builds.Dec 28 2021, 6:52 AM
This revision was automatically updated to reflect the committed changes.
anna added a subscriber: anna.Jan 4 2022, 7:16 AM

@spatel: inline comment added about incorrect arguments for one of the callsites. Pls fix.

llvm/lib/Analysis/ValueTracking.cpp
7149

just noticed this while reading the code. The bool arguments are swapped here. Should be:

computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false, UseInstrInfo, AC, I, DT, Depth + 1);
spatel added inline comments.Jan 4 2022, 10:16 AM
llvm/lib/Analysis/ValueTracking.cpp
7149

Thanks for reviewing!
1e50d064666f