www.digitalmars.com         C & C++   DMDScript  

D.gnu - Bad codegen for comparison with postfix increment

reply Paul Backus <snarwin gmail.com> writes:
When the following program is compiled with GDC 14.1, the 
assertion fails:

     void main() {
         int d = 42;
         bool o = d > d++;
         assert(o == false);
     }

(Godbolt: https://d.godbolt.org/z/e63xbb9Td)

The assertion passes when the program is compiled with DMD 
2.108.0.

The same bug was recently reported for LDC: 
https://github.com/ldc-developers/ldc/issues/4651
May 08
parent reply Iain Buclaw <ibuclaw gdcproject.org> writes:
On Wednesday, 8 May 2024 at 14:26:25 UTC, Paul Backus wrote:
 When the following program is compiled with GDC 14.1, the 
 assertion fails:

     void main() {
         int d = 42;
         bool o = d > d++;
         assert(o == false);
     }

 (Godbolt: https://d.godbolt.org/z/e63xbb9Td)

 The assertion passes when the program is compiled with DMD 
 2.108.0.

 The same bug was recently reported for LDC: 
 https://github.com/ldc-developers/ldc/issues/4651
I want to say that rewriting `d > d++` to `d++ < d` is a valid optimization. See also `opCmp` rewriting `a < b` into `b.opCmp(a)` https://dlang.org/spec/operatoroverloading.html#compare
May 09
parent Iain Buclaw <ibuclaw gdcproject.org> writes:
On Thursday, 9 May 2024 at 18:24:12 UTC, Iain Buclaw wrote:
 On Wednesday, 8 May 2024 at 14:26:25 UTC, Paul Backus wrote:
 When the following program is compiled with GDC 14.1, the 
 assertion fails:

     void main() {
         int d = 42;
         bool o = d > d++;
         assert(o == false);
     }

 (Godbolt: https://d.godbolt.org/z/e63xbb9Td)

 The assertion passes when the program is compiled with DMD 
 2.108.0.

 The same bug was recently reported for LDC: 
 https://github.com/ldc-developers/ldc/issues/4651
I want to say that rewriting `d > d++` to `d++ < d` is a valid optimization. See also `opCmp` rewriting `a < b` into `b.opCmp(a)` https://dlang.org/spec/operatoroverloading.html#compare
Even when it doesn't swap order, DMD doesn't store the left hand of the expression to a target/temporary when operator overloading is involved. https://d.godbolt.org/z/jYYr1WMhn ``` o = d.opCmp(d.opUnary!"++"()) ``` Effectively ``` arg = d.opUnary!"++"(); // d mutates o = d.opCmp(arg) > 0; // true ```
May 09