D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4034 - pure function calls returning a double do not seem to be optimized away
Summary: pure function calls returning a double do not seem to be optimized away
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords: performance
Depends on:
Blocks:
 
Reported: 2010-03-31 09:45 UTC by bearophile_hugs
Modified: 2024-12-13 17:52 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-03-31 09:45:44 UTC
This small D2 program calls two times a function msin that I have tagged as pure, but in the binary there are two sin instructions, I don't understand:


import std.c.stdio: printf;
import std.c.stdlib: atof;
import std.math: sin;
pure double msin(double x) {
    return sin(x);
}
void main() {
    double x = atof("0.3");
    double y = msin(x) + msin(x);
    printf("%f\n", y);
}


Compiled with dmd v2.042:
dmd -O -release -inline bug1.d

Produces:

_D4bug14msinFNadZd:
        fld qword ptr 4[ESP]
        fsin
        ret 8

__Dmain:
L0:     sub ESP,01Ch
        mov EAX,offset FLAT:_DATA
        push    EAX
        call    near ptr _atof
        mov ECX,offset FLAT:_DATA[4]
        fstp    qword ptr 8[ESP]
        fld qword ptr 8[ESP]
        fsin
        fld qword ptr 8[ESP]
        sub ESP,8
        fsin
        faddp   ST(1),ST
        fstp    qword ptr [ESP]
        push    ECX
        call    near ptr _printf
        add ESP,010h
        add ESP,01Ch
        xor EAX,EAX
        ret


Similar code is produced with this line:
double y = sin(x) + sin(x);
Comment 1 Dennis 2021-09-04 12:51:07 UTC
I don't think this is about returning double, but about `msin` not being `nothrow`.
Comment 2 dlangBugzillaToGithub 2024-12-13 17:52:02 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18211

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB