[Bug tree-optimization/53804] New: branch reordering missed optimization

steven at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Jun 29 10:36:00 GMT 2012


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)



More information about the Gcc-bugs mailing list