Bug 2923 – -O generates bad code for ?:

Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2009-05-02T13:45:00Z
Last change time
2014-04-18T09:12:02Z
Keywords
wrong-code
Assigned to
nobody
Creator
xurux1-mail

Comments

Comment #0 by xurux1-mail — 2009-05-02T13:45:37Z
adding the -inline option produces wrong output, both with Digital Mars D Compiler v1.043, Digital Mars D Compiler v2.029 on windows. import std.stdio; //writefln int my_abs(int i) { return i<0 ? -i : i; } void main(char[][] args) { int rows=9; int mid=5; for (int j=0; j<rows; j++) { int leadSpaces=my_abs((mid-1)-j); for (int i=0; i<leadSpaces; i++) writef("-"); //writefln("%d:%d",j,leadSpaces); writefln("!"); } } /* CORRECT: C:\dev\d>dmd -release -O -run compiler_bug1.d ----! ---! --! -! ! -! --! ---! ----! WRONG: C:\dev\d>dmd -release -O -inline -run compiler_bug1.d ! ! ! ! ! ! ! ! ! */
Comment #1 by clugdbug — 2009-05-15T08:40:41Z
Reduced test case: dmd bug.d //OK dmd -O bug.d // fails. Raising severity -- this is a VERY nasty bug. All the outer for loop does is prevent constant folding. With -O enabled, the wrong branch of ?: gets taken. void main() { for (int j=1; j<2; j++) { int x = (j<0) ? -j : j; int q=0; for (int i=0; i<x; i++) ++q; assert(q!=0); } }
Comment #2 by bugzilla — 2009-07-09T02:49:09Z
Fixed dmd 1.046 and 2.031