This is an archive of the discontinued LLVM Phabricator instance.

Introduce ConstantRange.addWithNoSignedWrap
ClosedPublic

Authored by apilipenko on Oct 18 2016, 10:43 AM.

Diff Detail

Repository
rL LLVM

Event Timeline

apilipenko retitled this revision from to Introduce ConstantRange.addWithNoSignedWrap.
apilipenko updated this object.
apilipenko added reviewers: sanjoy, reames.
apilipenko added a subscriber: llvm-commits.
sanjoy accepted this revision.Oct 18 2016, 11:02 AM
sanjoy edited edge metadata.

I think the algorithm in ConstantRange::addWithNoSignedWrap can be improved (see inline), but what you have here looks correct and most likely precise enough for the cases people will care about.

So I'd say let's move ahead with what's here and improve the implementation ConstantRange::addWithNoSignedWrap in parallel with working on the patches that will use this addWithNoSignedWrap interface.

lib/IR/ConstantRange.cpp
682 ↗(On Diff #75030)

Nice trick!

Nit: I'd use auto here.

However, this isn't precise for the following case (in i8):

Other = 20
*this = [50, -50)

Then I think your algorithm will have

NSWRange = [128, 108)
NSWConstrainedRange = full set // I've not verified this
Result = full set

However, we could have returned [70, -30) (naively, without considering the nsw) or [-108, 128), both of which are more precise.

I'm now wondering if the best algorithm is actually to first add Other to *this and then subtract out the portion that could not have resulted from an overflowing add (e.g. adding 5 rules out the result from containing [INT_MIN, INT_MIN + 5)).

This revision is now accepted and ready to land.Oct 18 2016, 11:02 AM
This revision was automatically updated to reflect the committed changes.