public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/53804] New: branch reordering missed optimization
@ 2012-06-29 10:36 steven at gcc dot gnu.org
  2012-06-29 10:43 ` [Bug tree-optimization/53804] " steven at gcc dot gnu.org
  2013-12-26 23:43 ` steven at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: steven at gcc dot gnu.org @ 2012-06-29 10:36 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 53804
           Summary: branch reordering missed optimization
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: steven@gcc.gnu.org


Consider this test case:

int
foo1 (int a, int b)
{
  if (a > 0)
    return 1;
  else if (b > 0 && a < 0)
    return -3;
  return 9;
}

int
foo2 (int a, int b)
{
  if (a > 0)
    return 1;
  else if (a < 0 && b > 0)
    return -3;
  return 9;
}


Ideally these two functions would be optimized to the same code, because they
are semantically equivalent. The ideal form is foo2 because the result of the
first comparison against "a" can be re-used for the second test, but GCC does
not perform this optimization. The .227r.final dump looks like this on
powerpc64-unknown-linux-gnu (all notes removed for readability):


;; Function foo1 (foo1, funcdef_no=0, decl_uid=1997, cgraph_uid=0)

   11 %7:CC=cmp(%3:SI,0)            # r7 = cmp(a,0)
    5 %9:DI=0x1                    # r9 = 1
   12 pc={(%7:CC<=0)?L74:pc}            # if (r7 <= 0) goto L74
L20:
   26 %3:DI=%9:DI                # r3 = r9
   29 use %3:DI                    # ..
   64 return                    # return r3
i  63: barrier
L74:
   14 %7:CC=cmp(%4:SI,0)            # r7 = cmp (b,0)
    8 %9:DI=0x9                    # r9 = 9
   15 pc={(%7:CC<=0)?L20:pc}            # if (r7 <= 0) goto L20
   53 %9:DI=-%3:DI==0                # r9 = -(r3==0)
   54 {%9:DI=%9:DI&0xc;clobber scratch;}    # r9 = r9 & 12
   55 %9:DI=%9:DI-0x3                # r9 = r9 - 3
   68 %3:DI=%9:DI                # r3 = r9
   69 use %3:DI                    # ..
   70 return                    # return r3
i  73: barrier

;; Function foo2 (foo2, funcdef_no=1, decl_uid=2001, cgraph_uid=1)

   11 %7:CC=cmp(%3:SI,0)            # r7 = cmp(a,0)
   12 pc={(%7:CC<=0)?L57:pc}            # if (r7 <= 0) goto L57
    5 %3:DI=0x1                    # r3 = 1
   29 use %3:DI                    # ..
   56 return                    # return r3
i  55: barrier
L57:
   14 %7:CC=cmp(%3:DI,0)            # r7 = cmp(a,0) // ??? redundant
    8 %3:DI=0x9                    # r3 = 9
   51 use %3:DI                    # ..
   15 pc={(%7:CC==0)?return:pc}            # if (r7 == 0) return r3
   17 %7:CC=cmp(%4:SI,0)            # r7 = cmp(b,0)
   52 use %3:DI                    # ..
   18 pc={(%7:CC<=0)?return:pc}            # if (r7 <= 0) return r3
    6 %3:DI=0xfffffffffffffffd            # r3 = -3
   53 use %3:DI                    # ..
   54 return                    # return r3
i  47: barrier


Note how foo1 needs two branches whereas foo2 only needs 1. 
(I'm not sure why there is the redundant compare in foo2:insn 14)


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

* [Bug tree-optimization/53804] branch reordering missed optimization
  2012-06-29 10:36 [Bug tree-optimization/53804] New: branch reordering missed optimization steven at gcc dot gnu.org
@ 2012-06-29 10:43 ` steven at gcc dot gnu.org
  2013-12-26 23:43 ` steven at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: steven at gcc dot gnu.org @ 2012-06-29 10:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Steven Bosscher <steven at gcc dot gnu.org> 2012-06-29 10:43:19 UTC ---
On x86_64-unknown-linux-gnu, this case is optimized because the second branch
condition is combined for both foo1 and foo2:

<bb 3>:
  D.1723_4 = b_3(D) > 0;
  D.1724_5 = a_2(D) < 0;
  D.1725_6 = D.1724_5 & D.1723_4;
  if (D.1725_6 != 0)
    goto <bb 4>;
  else
    goto <bb 5>;

=>

foo1:
        testl   %edi, %edi
        movl    $1, %eax
        jle     .L9
        rep
        ret
.L9:
        shrl    $31, %edi
        testb   %dil, %dil
        jne     .L10
.L4:
        movl    $9, %eax
        ret
.L10:
        testl   %esi, %esi
        jle     .L4
        movl    $-3, %eax
        ret


foo2:
        testl   %edi, %edi
        movl    $1, %eax
        jle     .L18
        rep
        ret
.L18:
        shrl    $31, %edi
        testb   %dil, %dil
        jne     .L19
.L14:
        movl    $9, %eax
        ret
.L19:
        testl   %esi, %esi
        jle     .L14
        movl    $-3, %eax
        ret


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

* [Bug tree-optimization/53804] branch reordering missed optimization
  2012-06-29 10:36 [Bug tree-optimization/53804] New: branch reordering missed optimization steven at gcc dot gnu.org
  2012-06-29 10:43 ` [Bug tree-optimization/53804] " steven at gcc dot gnu.org
@ 2013-12-26 23:43 ` steven at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: steven at gcc dot gnu.org @ 2013-12-26 23:43 UTC (permalink / raw)
  To: gcc-bugs

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

Steven Bosscher <steven at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
             Target|                            |powerpc*-*-*
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2013-12-26
     Ever confirmed|0                           |1
      Known to fail|                            |4.7.0, 4.8.0, 4.9.0


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

end of thread, other threads:[~2013-12-26 23:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-29 10:36 [Bug tree-optimization/53804] New: branch reordering missed optimization steven at gcc dot gnu.org
2012-06-29 10:43 ` [Bug tree-optimization/53804] " steven at gcc dot gnu.org
2013-12-26 23:43 ` steven 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).