https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102795

            Bug ID: 102795
           Summary: RFE: recognize !! vs branch similarity better
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jengelh at inai dot de
  Target Milestone: ---

Input
=====

unsigned long p1(unsigned long x)
{
        return x - x / 10 - !!(x % 10);
}
unsigned long p2(unsigned long x)
{
        if (x % 10 == 0)
                return x - x / 10;
        return x - x / 10 - 1;
}
unsigned long p3(unsigned long x)
{
        unsigned long z = x - x / 10;
        if (x % 10)
                --z;
        return z;
}

Outcome
=======

► ./host-x86_64-pc-linux-gnu/gcc/xg++ -B ./host-x86_64-pc-linux-gnu/gcc -c -O3
-v t.cpp
GNU C++17 (GCC) version 12.0.0 20211016 (experimental) (x86_64-pc-linux-gnu)
        compiled by GNU C version 11.2.1 20210816 [revision
056e324ce46a7924b5cf10f61010cf9dd2ca10e9], GMP version 6.2.1, MPFR version
4.1.0-p7, MPC version 1.2.1, isl version none
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096

► objdump -Mintel -d t.o
0000000000000000 <_Z2p1m>:
...
  24:   0f 95 c2                setne  dl
  27:   0f b6 d2                movzx  edx,dl
  2a:   48 29 d0                sub    rax,rdx
...

0000000000000030 <_Z2p2m>:
...
  4f:   48 29 d6                sub    rsi,rdx
  52:   48 29 d0                sub    rax,rdx
  55:   48 01 c9                add    rcx,rcx
  58:   48 39 cf                cmp    rdi,rcx
  5b:   48 0f 44 c6             cmove  rax,rsi
...

0000000000000060 <_Z2p3m>:
...
  81:   48 29 d7                sub    rdi,rdx
  84:   48 83 ff 01             cmp    rdi,0x1
  88:   48 83 d0 ff             adc    rax,0xffffffffffffffff
...


Expected outcome
================

I would have hoped that the optimizer were able to reduce p1, p2 and p3 to the
same asm.

Reply via email to