Bug 13976 – Value range propagation to disable some slice bound tests

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-01-13T12:56:00Z
Last change time
2015-02-18T03:42:39Z
Keywords
performance, pull
Assigned to
nobody
Creator
k.hara.pg

Comments

Comment #0 by k.hara.pg — 2015-01-13T12:56:05Z
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.
Comment #1 by k.hara.pg — 2015-01-13T13:08:25Z
Comment #2 by github-bugzilla — 2015-01-14T21:04:07Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/d80902bc7afd3be44249b6a15c97afdb42cf79bc fix Issue 13976 - Value range propagation to disable some slice bound tests https://github.com/D-Programming-Language/dmd/commit/68260d6cbaff13e93257bdba97d9d868a76b0dda Merge pull request #4293 from 9rnsr/fix13976 Issue 13976 - Value range propagation to disable some slice bound tests
Comment #3 by per.nordlow — 2015-01-15T18:33:31Z
Does this include avoiding range checking in expressions such as ```D x[0 .. $/2, $2..$] ``` which is a reoccurring pattern in D-style binary divide-and-conquer algorithms. It would be super-cool if it worked for rational variants with slice indexes in the form ```D $*p/q ``` where it can be statically proven that `p <= q`.
Comment #4 by per.nordlow — 2015-01-16T13:15:02Z
Correction: x[0 .. $/2, $2..$] should of course be x[0 .. $/2, $/2..$]
Comment #5 by github-bugzilla — 2015-02-18T03:42:39Z