Bug 20162 – Sign Extension for ?: optimization done wrong

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2019-08-24T14:53:24Z
Last change time
2020-08-09T12:01:12Z
Keywords
pull, wrong-code
Assigned to
No Owner
Creator
Yui Hosaka

Comments

Comment #0 by hos — 2019-08-24T14:53:24Z
The following code produces weird results: ---- import std.stdio; long f(long a) { return a % 1000000; } void main() { foreach (i; 0 .. 2) { writeln(typeid((i != 0) ? -1 : +1)); writeln(i, " ", ((i != 0) ? -1 : +1), " ", f((i != 0) ? -1 : +1)); } foreach (i; 0 .. 2) { foreach (j; 0 .. 2) { writeln(typeid((i != 0) ? -1 : +1)); writeln(i, " ", j, " ", ((i != 0) ? -1 : +1), " ", f((i != 0) ? -1 : +1)); } } } ---- (https://ideone.com/kyPT72) Its outputs are: int 0 1 1 int 1 -1 -1 int 0 0 1 1 int 0 1 1 1 int 1 0 -1 967295 int 1 1 -1 967295 printing (2^32 - 1) mod 1000000 instead of -1, only for the nested for-loop. This happens only with -m64 and -O options.
Comment #1 by bugzilla — 2020-08-07T09:01:22Z
A smaller test case: --- import core.stdc.stdio; long f(long a) { return a; } void main() { foreach (i; 1 .. 2) { foreach (j; 0 .. 2) { printf("%d %d %llx\n", i, ((i != 0) ? -1 : +1), f((i != 0) ? -1 : +1)); } } } --- which prints: 1 -1 ffffffff 1 -1 ffffffff
Comment #2 by dlang-bot — 2020-08-07T09:36:14Z
@WalterBright created dlang/dmd pull request #11529 "fix Issue 20162 - Sign Extension for ?: optimization done wrong" fixing this issue: - fix Issue 20162 - Sign Extension for ?: optimization done wrong https://github.com/dlang/dmd/pull/11529
Comment #3 by dlang-bot — 2020-08-09T12:01:12Z
dlang/dmd pull request #11529 "fix Issue 20162 - Sign Extension for ?: optimization done wrong" was merged into master: - a8e77a7de838efa7132179436cce26af047f9ab4 by Walter Bright: fix Issue 20162 - Sign Extension for ?: optimization done wrong https://github.com/dlang/dmd/pull/11529