Bug 13383 – wrong code with -O with ints, longs and bitwise operations

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2014-08-27T13:47:00Z
Last change time
2014-09-04T13:50:52Z
Keywords
pull, wrong-code
Assigned to
yebblies
Creator
gassa

Attachments

IDFilenameSummaryContent-TypeSize
1390example.dA small example demonstrating the problem.text/plain124

Comments

Comment #0 by gassa — 2014-08-27T13:47:47Z
Consider the following code (minimized example): ----- void main () { foreach (k; 32..33) { if (1L & (1L << k)) { assert (false); } } } ----- Obviously, 1L & (1L << k) must be true only when k = 0. Still, with k = 32, it produces a true value when compiled with "dmd -O". All is well without "-O". It is reproducible for me with DMD 2.064.2-2.066.0 on Win32. It works for me with DMD 2.063.2. So, the issue was probably introduced between 2.063.2 and 2.064.2.
Comment #1 by gassa — 2014-08-27T13:48:40Z
Created attachment 1390 A small example demonstrating the problem.
Comment #2 by yebblies — 2014-08-31T09:04:20Z
The compiler incorrectly optimizes 1L & (1L << k) to 1 btst k, which is only valid when 1L fits in a register. https://github.com/D-Programming-Language/dmd/pull/3934
Comment #3 by github-bugzilla — 2014-09-02T00:03:37Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/e14242906651c07247e41e37de1d27a18b81e54b Fix Issue 13383 - wrong code with -O with ints, longs and bitwise operations Converting '(1 << a) & b' to 'b btst a' is only valid when the constant fits in a register. https://github.com/D-Programming-Language/dmd/commit/10edd8d6121ffffd916bc94768723b15c6a321c1 Merge pull request #3934 from yebblies/issue13383 Issue 13383 - wrong code with -O with ints, longs and bitwise operations
Comment #4 by github-bugzilla — 2014-09-04T13:50:52Z
Commit pushed to 2.066 at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/d1a3b4e624971de83456d1b1e2a84dc480e54e94 Merge pull request #3934 from yebblies/issue13383 Issue 13383 - wrong code with -O with ints, longs and bitwise operations