This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug middle-end/16541] New: code quality issue for bit manipulations


The function below is from crafty, from the current version of crafty, not the
one in SPEC2000, but they are functionally identical. 

extern unsigned char first_one[65536];

int FirstOne(unsigned long long arg1)
{
  if (arg1 >> 48)
    return (first_one[arg1 >> 48]);
  if ((arg1 >> 32) & 65535)
    return (first_one[(arg1 >> 32) & 65535] + 16);
  if ((arg1 >> 16) & 65535)
    return (first_one[(arg1 >> 16) & 65535] + 32);
  return (first_one[arg1 & 65535] + 48);
}

The code mainline generates for this with -O2 on x86 is: 

        pushl   %ebp
        xorl    %edx, %edx
        movl    %esp, %ebp
        subl    $12, %esp
        movl    %ebx, (%esp)
        movl    %edx, %ecx
        movl    %esi, 4(%esp)
        movl    %edi, 8(%esp)
        movl    12(%ebp), %edi
        movl    8(%ebp), %esi
        movl    %edi, %eax
        shrl    $16, %eax
        orl     %eax, %ecx
        je      .L2
        movzbl  first_one(%eax), %eax
        movl    (%esp), %ebx
        movl    4(%esp), %esi
        movl    8(%esp), %edi
        movl    %ebp, %esp
        popl    %ebp
        ret
        .p2align 4,,7
.L2:
        movl    %edi, %eax
        xorl    %ebx, %ebx
        movzwl  %ax,%ecx
        movl    %ebx, %eax
        orl     %ecx, %eax
        je      .L5
        movzbl  first_one(%ecx), %eax
        movl    (%esp), %ebx
        movl    4(%esp), %esi
        movl    8(%esp), %edi
        movl    %ebp, %esp
        popl    %ebp
        addl    $16, %eax
        ret
        .p2align 4,,7
.L5:
        movl    %esi, %eax
        xorl    %ebx, %ebx
        shrdl   $16, %edi, %eax
        movzwl  %ax,%ecx
        movl    %ebx, %eax
        orl     %ecx, %eax
        je      .L7
        movzbl  first_one(%ecx), %eax
        movl    (%esp), %ebx
        movl    4(%esp), %esi
        movl    8(%esp), %edi
        movl    %ebp, %esp
        popl    %ebp
        addl    $32, %eax
        ret
        .p2align 4,,7
.L7:
        movl    %esi, %eax
        movl    (%esp), %ebx
        andl    $65535, %eax
        movl    4(%esp), %esi
        movzbl  first_one(%eax), %eax
        movl    8(%esp), %edi
        movl    %ebp, %esp
        popl    %ebp
        addl    $48, %eax
        ret

The code generated on SPARC for this function can be seen in PR16532

Intel's compiler  generates much nicer code: 

# parameter 1: 4 + %esp
..B1.1:                         # Preds ..B1.0
        movl      8(%esp), %edx                                 #3.5
        movl      %edx, %ecx                                    #5.15
        shrl      $16, %ecx                                     #5.15
        xorl      %eax, %eax                                    #5.3
        orl       %ecx, %eax                                    #5.3
        jne       ..B1.7        # Prob 28%                      #5.3
                                # LOE edx ecx ebx ebp esi edi
..B1.2:                         # Preds ..B1.1
        movzwl    %dx, %edx                                     #7.22
        xorl      %eax, %eax                                    #7.3
        orl       %edx, %eax                                    #7.3
        jne       ..B1.6        # Prob 28%                      #7.3
                                # LOE edx ebx ebp esi edi
..B1.3:                         # Preds ..B1.2
        movl      4(%esp), %edx                                 #9.16
        shrl      $16, %edx                                     #9.16
        xorl      %eax, %eax                                    #9.3
        orl       %edx, %eax                                    #9.3
        je        ..B1.5        # Prob 50%                      #9.3
                                # LOE edx ebx ebp esi edi
..B1.4:                         # Preds ..B1.3
        movzbl    first_one(%edx), %eax                         #10.13
        addl      $32, %eax                                     #10.47
        ret                                                     #10.47
                                # LOE
..B1.5:                         # Preds ..B1.3
        movl      4(%esp), %edx                                 #11.28
        movzwl    %dx, %ecx                                     #11.28
        movzbl    first_one(%ecx), %eax                         #11.11
        addl      $48, %eax                                     #11.37
        ret                                                     #11.37
                                # LOE
..B1.6:                         # Preds ..B1.2
        movzbl    first_one(%edx), %eax                         #8.13
        addl      $16, %eax                                     #8.47
        ret                                                     #8.47
                                # LOE
..B1.7:                         # Preds ..B1.1
        movzbl    first_one(%ecx), %eax                         #6.13
        ret                                                     #6.13

-- 
           Summary: code quality issue for bit manipulations
           Product: gcc
           Version: 3.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: middle-end
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dann at godzilla dot ics dot uci dot edu
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16541


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]