Array bound checks removal increasing importance

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Sat May 31 03:56:05 PDT 2014


My opinions about D array bound checks are slowly changing. I 
still think "-boundscheck=off" is useful and good to have. But 
now I am giving increasing importance to compiler logic that 
optimizes away bound checks safely. People more and more want a 
safe system language, more and more persons don't disable array 
bound tests. This means that optimizing away bound checks is 
becoming more and more important in D. And D can't ignore this 
need any more. Even adding logic to remove 20% of the bound 
checks in numeric code is going to help D because I think more 
and more people will not disable bound checks in D.


The following notes are derived from a post by Chris in D.learn:
http://forum.dlang.org/thread/kwkccgdymkpbpyzolumy@forum.dlang.org


Is it possible to optimize away all array bound checks in code 
like this?

void main() {
     int[5] data;
     foreach (const i; 0 .. 10)
         data[i] = 0;
     foreach (immutable i; 0 .. 10)
         data[i] = 0;
     int[10] big;
     foreach (const i, x; big)
         data[i] = x;
}


But the compiler must recognize this as correct code:


void main() {
     int[5] data;
     foreach (const i; 0 .. 10)
         if (i < 5)
             data[i] = 0;
}


Can this logic be added to D?


More info on the topic:
http://en.wikipedia.org/wiki/Bounds-checking_elimination
http://ssw.jku.at/Research/Papers/Wuerthinger07/Wuerthinger07.pdf

Bye,
bearophile


More information about the Digitalmars-d mailing list