D.gnu - Bad codegen for comparison with postfix increment
- Paul Backus (12/12) May 08 When the following program is compiled with GDC 14.1, the
- Iain Buclaw (5/17) May 09 I want to say that rewriting `d > d++` to `d++ < d` is a valid
- Iain Buclaw (13/34) May 09 Even when it doesn't swap order, DMD doesn't store the left hand
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
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/4651I 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
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: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 ```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/4651I 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