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; 10+ 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] 10+ messages in thread

* [Bug tree-optimization/24568] Missed optimization: trivialization of silly code
  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 ` pinskia at gcc dot gnu dot org
  2005-10-28 18:06 ` pinskia at gcc dot gnu dot org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-10-28 18:01 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2005-10-28 18:01 -------
There is a couple of different bugs in the is code snipit really.  Let me file
new bugs for each of them.


-- 


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/24568] Missed optimization: trivialization of silly code
  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
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-10-28 18:06 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2005-10-28 18:05 -------
(In reply to comment #1)
> There is a couple of different bugs in the is code snipit really.  Let me file
> new bugs for each of them.

PR 24574 is the first issue which seems like it could happen in really code
(though not as much as one thinks).


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  BugsThisDependsOn|                            |24574


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/24568] Missed optimization: trivialization of silly code
  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
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-10-28 18:10 UTC (permalink / raw)
  To: gcc-bugs



-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/24568] Missed optimization: trivialization of silly code
  2005-10-28 16:15 [Bug tree-optimization/24568] New: Missed optimization: trivialization of silly code tobi at gcc dot gnu dot org
                   ` (2 preceding siblings ...)
  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
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-10-28 18:11 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from pinskia at gcc dot gnu dot org  2005-10-28 18:11 -------
(In reply to comment #2)
> PR 24574 is the first issue which seems like it could happen in really code
> (though not as much as one thinks).

That was for the inner if.

I have to see how to reduce the outer ones.


-- 


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/24568] Missed optimization: trivialization of silly code
  2005-10-28 16:15 [Bug tree-optimization/24568] New: Missed optimization: trivialization of silly code tobi at gcc dot gnu dot org
                   ` (3 preceding siblings ...)
  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
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-10-28 18:15 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from pinskia at gcc dot gnu dot org  2005-10-28 18:15 -------
Another issue is PR 24575:
-(-a/10) is not converted into a/10.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  BugsThisDependsOn|                            |24575


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/24568] Missed optimization: trivialization of silly code
  2005-10-28 16:15 [Bug tree-optimization/24568] New: Missed optimization: trivialization of silly code tobi at gcc dot gnu dot org
                   ` (4 preceding siblings ...)
  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
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-10-28 18:18 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from pinskia at gcc dot gnu dot org  2005-10-28 18:18 -------
Last but not least is:
int f(int i)
{
  if (i < 0) return i/10;
  return i/10;
}


int f2(int i)
{
  return i/10;
}

Which is converted at the rtl level but not at the tree level (I think there is
a bug about this already).


-- 


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/24568] [meta-bugs] Missed optimization: trivialization of silly code
  2005-10-28 16:15 [Bug tree-optimization/24568] New: Missed optimization: trivialization of silly code tobi at gcc dot gnu dot org
                   ` (5 preceding siblings ...)
  2005-10-28 18:18 ` pinskia at gcc dot gnu dot org
@ 2005-10-28 18:20 ` 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
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-10-28 18:20 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from pinskia at gcc dot gnu dot org  2005-10-28 18:19 -------
Confirmed, I am changing this into a meta-bug because there are three different
issues that this bug depends on now.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2005-10-28 18:19:55
               date|                            |


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/24568] [meta-bugs] Missed optimization: trivialization of silly code
  2005-10-28 16:15 [Bug tree-optimization/24568] New: Missed optimization: trivialization of silly code tobi at gcc dot gnu dot org
                   ` (6 preceding siblings ...)
  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
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-10-28 18:21 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from pinskia at gcc dot gnu dot org  2005-10-28 18:21 -------
Oh, for PR 24575 to do anything useful in this case, we need to a tree based
combiner.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  BugsThisDependsOn|                            |15459


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/24568] [meta-bug] Missed optimization: trivialization of silly code
  2005-10-28 16:15 [Bug tree-optimization/24568] New: Missed optimization: trivialization of silly code tobi at gcc dot gnu dot org
                   ` (7 preceding siblings ...)
  2005-10-28 18:21 ` pinskia at gcc dot gnu dot org
@ 2007-03-29 12:29 ` steven at gcc dot gnu dot org
  8 siblings, 0 replies; 10+ messages in thread
From: steven at gcc dot gnu dot org @ 2007-03-29 12:29 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from steven at gcc dot gnu dot org  2007-03-29 13:29 -------
For the original test case, our current output before expand (i.e. the
final_cleanup dump) on hosts where sizeof(long)==sizeof(int) is this:

;; Function convertToMinutes (convertToMinutes)

convertToMinutes (milliDiff)
{
  int minutesDiff.24;
  int minutesDiff;

<bb 2>:
  if (milliDiff < 0) goto <L0>; else goto <L1>;

<L0>:;
  minutesDiff = -milliDiff / 60000;
  minutesDiff.24 = -minutesDiff;
  goto <bb 7> (<L6>);

<L1>:;
  if (milliDiff == 0) goto <L13>; else goto <L3>;

<L13>:;
  minutesDiff.24 = 0;
  goto <bb 7> (<L6>);

Invalid sum of outgoing probabilities 0.0%
Invalid sum of incoming frequencies 2000, should be 0
<L3>:;
  minutesDiff.24 = milliDiff / 60000;

Invalid sum of incoming frequencies 8000, should be 10000
<L6>:;
  return minutesDiff.24;

}


Note that on hosts where sizeof(long) != sizeof(int), you can't optimize the
original test case to i/60000.


-- 

steven at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2006-02-26 19:10:03         |2007-03-29 13:29:35
               date|                            |


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2007-03-29 12:29 UTC | newest]

Thread overview: 10+ 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

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).