RFC: Value range propagation for if-else

Lionello Lunesu via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 20 04:53:28 PDT 2014


On 20/06/14 15:53, Don wrote:
> On Wednesday, 18 June 2014 at 06:40:21 UTC, Lionello Lunesu wrote:
>> Hi,
>
>> 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.
>
>> Destroy?
>
> The compiler uses value range propagation in this {min, max} form, but I
> think that's an implementation detail. It's well suited for arithmetic
> operations, but less suitable for logical operations. For example, this
> code can't overflow, but {min, max} range propagation thinks it can.
>
> ubyte foo ( uint a) {
>    return (a & 0x8081) & 0x0FFF;
> }
>
> For these types of expressions, {known_one_bits, known_zero_bits} works
> better.
> Now, you can track both types of range propagation simultaneously, and I
> think we probably should improve our implementation in that way. It
> would improve the accuracy in many cases.
>
> Question: If we had implemented that already, would you still want the
> interface you're proposing here?
>

You could have different __traits in that case:
__traits(valueRange,...) // for min/max
__traits(bitRange,...) // mask

You example seems rather artificial though. IRL you'd get a compiler 
warning/error and could fix it by changing the code to "& 0xFF". I 
personally have not yet had the need for these bit-masks.

L.


More information about the Digitalmars-d mailing list