https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105360
Bug ID: 105360 Summary: Inlined lazy parameters / delegate literals, still emitted Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: d Assignee: ibuclaw at gdcproject dot org Reporter: witold.baryluk+gcc at gmail dot com Target Milestone: --- ``` extern bool g(); extern void f(int n); void log(lazy int num) { if (g()) { const n = num(); f(n); } } void p(int n) { log(n * 137); } ``` This should emit the same (or close to the same) as code with no `lazy` (and num reference changed accordingly) on `log` function. (Because compiler knows that `num ` is called once, has no side effects, is moderately expensive, etc). And the code for p is exactly the same - log and `n * 137` fully inlined. However, the anonymous dgliteral code is still emitted, despite not being referenced anywhere: ``` pure nothrow @nogc @safe int example.p(int).__dgliteral2(): # < This should not be in object file imul eax, DWORD PTR [rdi], 137 ret ``` Rest of the object file is correct and optimal: ``` void example.log(lazy int): push rbp push rbx mov rbp, rdi mov rbx, rsi sub rsp, 8 call bool example.g() test al, al je .L3 mov rdi, rbp call rbx add rsp, 8 pop rbx pop rbp mov edi, eax jmp void example.f(int) .L3: add rsp, 8 pop rbx pop rbp ret void example.p(int): push rbx mov ebx, edi call bool example.g() test al, al je .L6 imul edi, ebx, 137 pop rbx jmp void example.f(int) .L6: pop rbx ret ``` gdc (Compiler-Explorer-Build-gcc-748d46cd049c89a799f99f14547267ebae915af6-binutils-2.36.1) 12.0.1 20220421 (experimental) via godbolt.org For a code passing reasonably big literals, this can lead to object file code duplication. ldc2 shows no such problem.