RFC: Value range propagation for if-else

Manu via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 30 20:02:56 PDT 2014


On 18 June 2014 16:40, Lionello Lunesu via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
> Hi,
>
> I got this thing working and I think it's about time I get some comments on
> it.
>
> I've been wanting to extend Value Rang Propagation (VRE) for some time now.
> Mostly because of the fix to the troublesome "signed-unsigned comparisons"
> issue. Enabling VRE for if-else and "&&" will fix many of the false-positive
> warnings given out by the fix to bug 259 by allowing code like this: if
> (signed > 0 && signed < unsigned) { .. }
>
> Have a look at the branch:
> https://github.com/lionello/dmd/compare/if-else-range
>
> There, I've also added a __traits(intrange, <expression>) which returns a
> tuple with the min and max for the given expression. It's used in the test
> case as follows:
>
>
>     const i = foo ? -1 : 33;
>
>     if (i)
>       static assert(__traits(intrange, i) == Tuple!(-1, 33));
>     else
>     {
>       //static assert(i == 0); TODO
>       static assert(__traits(intrange, i) == Tuple!(0, 0));
>     }
>
>     if (i == 33)
>     {
>       //static assert(i == 33); TODO
>       static assert(__traits(intrange, i) == Tuple!(33, 33));
>     }
>     else
>       static assert(__traits(intrange, i) == Tuple!(-1, 32));
>
>     if (10 <= i)
>       static assert(__traits(intrange, i) == Tuple!(10, 33));
>     else
>       static assert(__traits(intrange, i) == Tuple!(-1, 9));
>
>
> It would be nice if this can be used by CTFE as well.
>
> Destroy?

I'm looking forward to this so hard!
The one time I've attempted to hack on DMD, it was to investigate the
idea of doing this.

As others said, I think a key use case is for contracts/preconditions.
Also eliminating annoying warnings when down-casting.

I suspect there are many great optimisation opportunities available
too when this is pervasive. switch() may gain a lot of great
information to work with! :)


More information about the Digitalmars-d mailing list