www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13976] New: Value range propagation to disable some slice

https://issues.dlang.org/show_bug.cgi?id=13976

          Issue ID: 13976
           Summary: Value range propagation to disable some slice bound
                    tests
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: k.hara.pg gmail.com

In general, with a slice expression:

  arr[a..b]

The array boundaries are checked by the condition at runtime:

  b <= arr.length && a <= b`

But with the following program:

void main()
{
    int[10] a;
    size_t n;
    auto s = a[n%3 .. n%3 + 3];
    assert(s.length == 3);
}

Compiler can deduce:

  ValueRangeOf(lower) == ValueRangeOf(n%3) --> [0, 2]
  ValueRangeOf(upper) == ValueRangeOf(n%3 + 3) --> [3, 5]
  a.length == 10

and those formulas are satisfied always:

  ValueRangeOf(upper) <= arr.length 
  ValueRangeOf(lower) <= ValueRangeOf(upper)

Finally, compiler can eliminate redundant runtime bound tests.

Note that, optimization for the array indexing is already done in issue 9097.

--
Jan 13 2015