public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/24568]  New: Missed optimization: trivialization of silly code
@ 2005-10-28 16:15 tobi at gcc dot gnu dot org
  2005-10-28 18:01 ` [Bug tree-optimization/24568] " pinskia at gcc dot gnu dot org
                   ` (8 more replies)
  0 siblings, 9 replies; 13+ messages in thread
From: tobi at gcc dot gnu dot org @ 2005-10-28 16:15 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 13+ messages in thread
[parent not found: <bug-24568-4@http.gcc.gnu.org/bugzilla/>]

end of thread, other threads:[~2021-07-27  2:41 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-28 16:15 [Bug tree-optimization/24568] New: Missed optimization: trivialization of silly code tobi at gcc dot gnu dot org
2005-10-28 18:01 ` [Bug tree-optimization/24568] " pinskia at gcc dot gnu dot org
2005-10-28 18:06 ` pinskia at gcc dot gnu dot org
2005-10-28 18:10 ` pinskia at gcc dot gnu dot org
2005-10-28 18:11 ` pinskia at gcc dot gnu dot org
2005-10-28 18:15 ` pinskia at gcc dot gnu dot org
2005-10-28 18:18 ` pinskia at gcc dot gnu dot org
2005-10-28 18:20 ` [Bug tree-optimization/24568] [meta-bugs] " pinskia at gcc dot gnu dot org
2005-10-28 18:21 ` pinskia at gcc dot gnu dot org
2007-03-29 12:29 ` [Bug tree-optimization/24568] [meta-bug] " steven at gcc dot gnu dot org
     [not found] <bug-24568-4@http.gcc.gnu.org/bugzilla/>
2014-10-31  4:02 ` pinskia at gcc dot gnu.org
2021-07-26 23:57 ` pinskia at gcc dot gnu.org
2021-07-27  2:41 ` tobi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).