www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5688] New: Poor optimization of (long & 1)

reply d-bugmail puremagic.com writes:
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



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: -------
Mar 03 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5688


Walter Bright <bugzilla digitalmars.com> changed:

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



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: -------
Mar 03 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5688





 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: -------
Mar 03 2011