Thread overview
[Issue 5688] New: Poor optimization of (long & 1)
Mar 03, 2011
Don
Mar 03, 2011
Walter Bright
Mar 04, 2011
Don
March 03, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5688

           Summary: Poor optimization of (long & 1)
           Product: D
           Version: D1 & D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: clugdbug@yahoo.com.au


--- Comment #0 from Don <clugdbug@yahoo.com.au> 2011-03-03 00:47:06 PST ---
The optimiser does a very poor job in a case like this:
bool foo(long v)
{
    return  v&1;
}


It generates this:
                mov     EAX,4[ESP]
                mov     EDX,8[ESP]
                and     EAX,1
                xor     EDX,EDX
                or      EDX,EAX
                jne     L17
                xor     EAX,EAX
                jmp short       L1C
L17:            mov     EAX,1
L1C:            ret     8

That's terrible code! It should just do:

mov EAX, 4[ESP]
and EAX, 1
ret 8

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 03, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5688


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com


--- Comment #1 from Walter Bright <bugzilla@digitalmars.com> 2011-03-03 11:49:26 PST ---
Interestingly, if the code is written as:

bool foo(long v)
{
    return  (v & 1) == 1;
}

the code generated is:

                mov     EAX,4[ESP]
                mov     EDX,8[ESP]
                and     EAX,1
                xor     EDX,EDX
                ret     8

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 04, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5688



--- Comment #2 from Don <clugdbug@yahoo.com.au> 2011-03-03 17:52:46 PST ---
(In reply to comment #1)
> Interestingly, if the code is written as:
> 
> bool foo(long v)
> {
>     return  (v & 1) == 1;
> }
> 
> the code generated is:
> 
>                 mov     EAX,4[ESP]
>                 mov     EDX,8[ESP]
>                 and     EAX,1
>                 xor     EDX,EDX
>                 ret     8

I noticed that. And even though that's better, both uses of EDX are completely
unnecessary.
Changing  cgelem.c, elcmp(), around line 3350 to this:

                    case 8:
-                        e = el_una(OP64_32,TYlong,e);
+                        e->E1 = el_una(OP64_32,TYint,e->E1);
+                        e->E2 = el_una(OP64_32,TYint,e->E2);
                        break;
makes it create optimal code, although that's probably incorrect for 64 bits.
The way elcmp() works looks pretty bizarre to me.
But it's the return ( v & 1); case that is the primary problem.

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