[Issue 11506] pure evaluation should be shortcircuited

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Nov 12 15:26:52 PST 2013


https://d.puremagic.com/issues/show_bug.cgi?id=11506


bearophile_hugs at eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs at eml.cc


--- Comment #2 from bearophile_hugs at eml.cc 2013-11-12 15:26:47 PST ---
(In reply to comment #0)
> Also:
> 
> pure int fun();
> 
> int main(string[] args)
> {
>     return fun() + fun();
> }
> 
> This program doesn't link, but the generated code reveals that fun() is called
> twice. It should only called once.

If I compile this code with:
dmd -O -release -noboundscheeck temp.d


int fun() pure nothrow { return 0; }
int main() {
   return fun + fun;
}


It contains only one call to fun (and add EAX,EAX doubles its result):

_D4temp3funFNaNbZi:
        xor EAX,EAX
        ret

__Dmain:
L0:     push    EAX
        call    near ptr _D4temp3funFNaNbZi
        add EAX,EAX
        pop ECX
        ret

------------------------

But if I remove the nothrow:

int fun() pure { return 0; }
int main() {
   return fun + fun;
}


It shows both calls to fun:

_D4temp3funFNaZi:
        xor EAX,EAX
        ret

__Dmain:
L0:     push    EAX
        call    near ptr _D4temp3funFNaZi
        push    EAX
        sub ESP,4
        call    near ptr _D4temp3funFNaZi
        add ESP,4
        mov ECX,EAX
        pop EAX
        add EAX,ECX
        pop ECX
        ret


The ability to throw is an effect (a side effect) and it can't be ignored. On
the other hand fun takes no arguments, so I think the two calls to fun fun
can't decide to throw or not throw independently. So I think calling fun only
once is OK even if it doesn't have a nothrow annotation.

On the other hand two calls to a a function foo like this:


int foo(in size_t x) pure {
    if (x & 1) throw new Exception("");
    return 10;
}
int main(in string[] args) {
   return fun(args.length) + fun(args.length);
}


Can't be replaced with a single call.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list