Bug 9097 – Value range propagation to disable some array bound tests
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-11-29T11:17:00Z
Last change time
2013-07-09T03:15:11Z
Keywords
pull
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2012-11-29T11:17:33Z
A little test program:
void main(string[] args) {
size_t a = 6000 + args.length;
int[200] array;
array[a % 100] = 1;
}
-----------------------
Compiled with "dmd -O -inline":
__Dmain:
L0: sub ESP, 0338h
mov ECX, 0C8h
push EDI
mov EAX, 0340h[ESP]
add EAX, 01770h
mov 0Ch[ESP], EAX
xor EAX, EAX
lea EDI, 01Ch[ESP]
rep
stosd
mov EAX, 0Ch[ESP]
mov ECX, 064h
xor EDX, EDX
div ECX
cmp EDX, 0C8h
mov 8[ESP], EDX
jb L47
mov EAX, 4
call near ptr _D5test27__arrayZ
L47: mov EDX, 8[ESP]
xor EAX, EAX
mov dword ptr 01Ch[EDX*4][ESP], 1
pop EDI
add ESP, 0338h
ret
_D5test27__arrayZ:
L0: push EAX
mov ECX, offset FLAT:_D5test212__ModuleInfoZ
push EAX
push ECX
call near ptr __d_array_bounds
-----------------------
Compiled with "dmd -O -inline -release -noboundscheck":
__Dmain:
sub ESP, 0330h
mov ECX, 0C8h
push EDI
mov EAX, 0338h[ESP]
add EAX, 01770h
mov 4[ESP], EAX
xor EAX, EAX
lea EDI, 014h[ESP]
rep
stosd
mov EAX, 4[ESP]
mov ECX, 064h
xor EDX, EDX
div ECX
xor EAX, EAX
mov dword ptr 014h[EDX*4][ESP], 1
pop EDI
add ESP, 0330h
ret
-----------------------
Value range propagation is able to tell that the value of "a % 100" can't be outside the 0..200 bounds of the array, so there is no point in performing the bound test even in nonrelease mode.
Comment #3 by bearophile_hugs — 2013-07-09T03:15:11Z
In past dmd generated this with "dmd -O -inline -release -noboundscheck":
__Dmain:
sub ESP, 0330h
mov ECX, 0C8h
push EDI
mov EAX, 0338h[ESP]
add EAX, 01770h
mov 4[ESP], EAX
xor EAX, EAX
lea EDI, 014h[ESP]
rep
stosd
mov EAX, 4[ESP]
mov ECX, 064h
xor EDX, EDX
div ECX
xor EAX, EAX
mov dword ptr 014h[EDX*4][ESP], 1
pop EDI
add ESP, 0330h
ret
Now it generates this in both the optimized build and the less optimized build "dmd -O -inline" (beside removing the array bound tests, it also avoids the "div"):
__Dmain:
sub ESP, 0324h
mov ECX, 0C8h
push EDI
mov EAX, 032Ch[ESP]
add EAX, 01770h
mov 0324h[ESP], EAX
xor EAX, EAX
lea EDI, 4[ESP]
rep
stosd
mov ECX, 0324h[ESP]
mov EAX, ECX
mov EDX, 051EB851Fh
mul EDX
shr EDX, 5
imul EAX, EDX, 064h
sub ECX, EAX
xor EAX, EAX
mov dword ptr 4[ECX*4][ESP], 1
pop EDI
add ESP, 0324h
ret