Bug 13190 – Optimizer breaks comparison with zero

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2014-07-22T17:53:00Z
Last change time
2015-06-09T05:11:46Z
Keywords
pull, wrong-code
Assigned to
yebblies
Creator
yebblies

Comments

Comment #0 by yebblies — 2014-07-22T17:53:01Z
This code, compiled with -release -O, breaks on win32 and linux32 (and presumably other32). The for loop comparison (table[i].a != 0) gets implemented as a load+or+bne, but the increment for the loop index gets inserted between the or and the loop, which trashes the flags register. This results in an infinite loop and eventually a segfault. extern(C) int printf(const(char)*,...); struct Struct { ulong a; uint b; }; __gshared Struct[] table = [ Struct(1, 1), Struct(0, 2) ]; void main() { for (int i = 0; table[i].a; i++) { ulong tbl = table[i].a; printf("i:%d stc:%lld\n", i, tbl); if (1 + tbl) { if (tbl == 0x80000) return; } } } condition asm: _003 == array base, ebx = current offset, esi = loop index ?_016: add ebx, 16 ; 005E _ 83. C3, 10 mov ecx, dword ptr [?_003] ; 0061 _ 8B. 0D, 00000034(segrel) mov edx, dword ptr [ebx+ecx+4H] ; 0067 _ 8B. 54 0B, 04 or edx, dword ptr [ebx+ecx] ; 006B _ 0B. 14 0B inc esi ; 006E _ 46 jnz ?_014 ; 006F _ 75, A4
Comment #1 by yebblies — 2014-07-23T11:17:21Z
Quick fix is to disable the block in cgsched.c that has comment "make sure CFpsw is reliably set". Turns out CFpsw is _not_ reliably set.
Comment #2 by yebblies — 2014-07-23T11:46:24Z
Comment #3 by github-bugzilla — 2014-07-24T10:03:20Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/e73d4ddd25a776106b1dd21ab330787cf7cc13c2 Fix Issue 13190 - Optimizer breaks comparison with zero https://github.com/D-Programming-Language/dmd/commit/9ceed605ec17cc337cd2d62a7eed131c969ac7f7 Merge pull request #3803 from yebblies/issue13190 [DDMD] Issue 13190 - Optimizer breaks comparison with zero
Comment #4 by github-bugzilla — 2014-07-24T10:06:10Z
Comment #5 by github-bugzilla — 2014-07-31T02:35:11Z
Commit pushed to 2.066 at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/38bb71c48e84cbe386d734fa501184bb39fa1e39 Merge pull request #3803 from yebblies/issue13190 [DDMD] Issue 13190 - Optimizer breaks comparison with zero
Comment #6 by github-bugzilla — 2014-08-22T08:04:58Z