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

            Bug ID: 110738
           Summary: GCC trunk failed to do some optimizations since GCC-8
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shaohua.li at inf dot ethz.ch
  Target Milestone: ---

For the following test case, gcc trunk at -O3 emits a relatively large assembly
code since GCC-8. Clang can remove all the code.

Compiler explorer: https://godbolt.org/z/hzf8Pr484

The test case is adapted from an official GCC test case:
https://github.com/gcc-mirror/gcc/blob/ae862e0e47cb2d62d7c624ab999a3bd8bd2914ef/gcc/testsuite/gcc.dg/strncmp-3.c

I only changed `int neg=0` to `int neg=1`. 

$ gcc-tk -O3 -S -o trunk.s a.c
$ gcc-7.4 -O3 -S -o old.s a.c
$ wc trunk.s old.s
 217  654 5999 trunk.s
  11   19  175 old.s
 228  673 6174 total
$
$ cat a.c
#define AB_D "ab\0d"
#define ABCDEF_H "abcdef\0h"
#define ABCDEFGHIJKLMN_P "abcdefghijklmn\0p"

char ab_d[] = AB_D;
char abcdef_h[] = ABCDEF_H;

extern int strncmp (const char*, const char*, __SIZE_TYPE__);

__attribute__((noipa)) void sink (const void *p, ...) { (void)&p; }

#define strncmp(a, b, n) (sink (a, b), strncmp (a, b, n))

int main (void)
{
  int zero = 0;

  zero += strncmp (ab_d, AB_D,1);
  zero += strncmp (ab_d, AB_D,2);
  zero += strncmp (ab_d, AB_D,3);
  zero += strncmp (ab_d, AB_D,4);
  zero += strncmp (ab_d, AB_D,5);

  zero += strncmp (ab_d, ABCDEF_H,1);
  zero += strncmp (ab_d, ABCDEF_H,2);

  zero += strncmp (abcdef_h, AB_D,2);

  zero += strncmp (abcdef_h, ABCDEF_H,2);
  zero += strncmp (abcdef_h, ABCDEF_H,3);
  zero += strncmp (abcdef_h, ABCDEF_H,4);
  zero += strncmp (abcdef_h, ABCDEF_H,5);
  zero += strncmp (abcdef_h, ABCDEF_H,6);
  zero += strncmp (abcdef_h, ABCDEF_H,7);
  zero += strncmp (abcdef_h, ABCDEF_H,8);
  zero += strncmp (abcdef_h, ABCDEF_H,9);
  if (zero != 0)
    __builtin_abort ();
  int neg = 1;
  neg -= strncmp (ab_d, ABCDEF_H,3) <0;
  neg -= strncmp (ab_d, ABCDEF_H,4) <0;
  neg -= strncmp (ab_d, ABCDEF_H,5) <0;
  neg -= strncmp (ab_d, ABCDEF_H,6) <0;
  neg -= strncmp (ab_d, ABCDEF_H,7) <0;
  neg -= strncmp (ab_d, ABCDEF_H,8) <0;
  neg -= strncmp (ab_d, ABCDEF_H,9) <0;
  if (neg !=-7){
    __builtin_abort ();
  }
}
$
$ gcc-tk -v
Using built-in specs.
COLLECT_GCC=gcc-tk
COLLECT_LTO_WRAPPER=/zdata/shaoli/compilers/ccbuilder-compilers/gcc-ec842611618c9f8a50f3326d42c51961de8b28a5/libexec/gcc/x86_64-pc-linux-gnu/14.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --disable-multilib --disable-bootstrap
--enable-languages=c,c++
--prefix=/zdata/shaoli/compilers/ccbuilder-compilers/gcc-ec842611618c9f8a50f3326d42c51961de8b28a5
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 14.0.0 20230719 (experimental) (GCC) 
$

Reply via email to