Thread overview
Bad codegen for comparison with postfix increment
May 08, 2024
Paul Backus
May 09, 2024
Iain Buclaw
May 10, 2024
Iain Buclaw
May 08, 2024

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 09, 2024

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 10, 2024

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