[Bug tree-optimization/24568] New: Missed optimization: trivialization of silly code

tobi at gcc dot gnu dot org gcc-bugzilla@gcc.gnu.org
Fri Oct 28 16:15:00 GMT 2005


xgcc (GCC) 4.1.0 20051020 (experimental)
can't optimize the following code (from <http://thedailywtf.com>):

int convertToMinutes(long milliDiff)
{
  int negative = 0;
  int minutesDiff = -1;

  if (milliDiff < 0)
  {
    negative = 1;
    milliDiff = -milliDiff; // Make positive (is easier)
  }

  if (milliDiff == 0) // Watch out for exceptional value 0
    minutesDiff = 0;
  else
    minutesDiff = (int) (milliDiff / 1000) / 60;

  if (negative) minutesDiff = -minutesDiff; // Make it negative again

  return minutesDiff;
}

The code should simplify to
int convertToMinutes (long i)
{
   return i / 60000;
}

But the final tree dump contains the complete logic even in the case of high
optimization levels.

An interesting peculiarity from the assembly:
        .file   "milli.c"
        .text
        .p2align 4,,15
.globl convertToMinutes
        .type   convertToMinutes, @function
convertToMinutes:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        movl    %esi, 4(%esp)
        movl    8(%ebp), %esi
        movl    %ebx, (%esp)
        xorl    %ebx, %ebx
        testl   %esi, %esi
        js      .L12          ;;;;; jump from here
.L4:
        xorl    %ecx, %ecx
        testl   %esi, %esi
        je      .L7
        movl    $1172812403, %eax
        imull   %esi
        movl    %esi, %eax
        sarl    $31, %eax
        movl    %edx, %ecx
        sarl    $14, %ecx
        subl    %eax, %ecx
.L7:
        testl   %ebx, %ebx
        je      .L8
        negl    %ecx
.L8:
        movl    (%esp), %ebx
        movl    %ecx, %eax
        movl    4(%esp), %esi
        movl    %ebp, %esp
        popl    %ebp
        ret
        .p2align 4,,7
.L12:    ;;;;;;;;;;; all the way down here, probably out of the prefetch buffer
        negl    %esi
        movl    $1, %ebx
        jmp     .L4
        .size   convertToMinutes, .-convertToMinutes
        .ident  "GCC: (GNU) 4.1.0 20051020 (experimental)"
        .section        .note.GNU-stack,"",@progbits


-- 
           Summary: Missed optimization: trivialization of silly code
           Product: gcc
           Version: 4.1.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P2
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: tobi at gcc dot gnu dot org
GCC target triplet: i686-pc-linux


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



More information about the Gcc-bugs mailing list