www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22158] New: Optimize away empty loops

https://issues.dlang.org/show_bug.cgi?id=22158

          Issue ID: 22158
           Summary: Optimize away empty loops
           Product: D
           Version: D2
          Hardware: x86_64
                OS: All
            Status: NEW
          Keywords: performance
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: bugzilla digitalmars.com
          Reporter: dlang-bugzilla thecybershadow.net

void fun(int a, int b)
{
    for (int i = a; i < b; i++) {}
}

With -O -inline -release, DMD compiles the above function to:

                push    RBP
                mov     RBP,RSP
                mov     ECX,ESI
                cmp     ESI,EDI
                jge     $+7h
                inc     ECX
                cmp     ECX,EDI
                jl      $+FFFFFFFBh
                pop     RBP
                ret

LDC and GDC (with their respective optimization flags on) both compile it to
just a "ret".

This optimization could help writing simpler code in performance-critical
areas, such as the GC:
https://github.com/dlang/druntime/pull/3523#discussion_r676094354

--
Jul 30 2021