public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/99011] New: Potentially missed optimization. Arrays are created without need
@ 2021-02-08 19:05 pj at hugeone dot co.uk
  2021-02-08 19:14 ` [Bug c/99011] " pj at hugeone dot co.uk
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: pj at hugeone dot co.uk @ 2021-02-08 19:05 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 99011
           Summary: Potentially missed optimization. Arrays are created
                    without need
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pj at hugeone dot co.uk
  Target Milestone: ---

Consider the code:

```
int bar(int N)
{
    return  N > 10 ? 14 : 8;
}

int foo(int N)
{
    return (const int[]){8, 14}[N > 10]; 
}


int zoo(int N)
{
    int a[] = {8,14};
    return a[N > 10]; 
}

int boo(int N)
{
    const static int a[] = {8,14};
    return a[N > 10]; 
}
```

IMO in all cases the generated code should be the same as generated for
function bar. IMO arrays can be optimized out.

Compiled with -O3

foo:
        mov     rax, QWORD PTR .LC0[rip]
        mov     QWORD PTR [rsp-8], rax
        xor     eax, eax
        cmp     edi, 10
        setg    al
        mov     eax, DWORD PTR [rsp-8+rax*4]
        ret
bar:
        cmp     edi, 10
        mov     edx, 8
        mov     eax, 14
        cmovle  eax, edx
        ret
zoo:
        mov     rax, QWORD PTR .LC0[rip]
        mov     QWORD PTR [rsp-8], rax
        xor     eax, eax
        cmp     edi, 10
        setg    al
        mov     eax, DWORD PTR [rsp-8+rax*4]
        ret
boo:
        xor     eax, eax
        cmp     edi, 10
        setg    al
        mov     eax, DWORD PTR a.0[0+rax*4]
        ret
a.0:
        .long   8
        .long   14
.LC0:
        .long   8
        .long   14


gcc -O3 --verbose:
Using built-in specs.
COLLECT_GCC=/opt/compiler-explorer/gcc-10.2.0/bin/gcc
Target: x86_64-linux-gnu
Configured with: ../gcc-10.2.0/configure
--prefix=/opt/compiler-explorer/gcc-build/staging --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu --disable-bootstrap
--enable-multiarch --with-abi=m64 --with-multilib-list=m32,m64,mx32
--enable-multilib --enable-clocale=gnu
--enable-languages=c,c++,fortran,ada,go,d --enable-ld=yes --enable-gold=yes
--enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-linker-build-id
--enable-lto --enable-plugins --enable-threads=posix
--with-pkgversion=Compiler-Explorer-Build
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.2.0 (Compiler-Explorer-Build) 
COLLECT_GCC_OPTIONS='-fdiagnostics-color=always' '-g' '-o' './output.s'
'-masm=intel' '-S' '-v' '-O3' '-mtune=generic' '-march=x86-64'

/opt/compiler-explorer/gcc-10.2.0/bin/../libexec/gcc/x86_64-linux-gnu/10.2.0/cc1
-quiet -v -imultiarch x86_64-linux-gnu -iprefix
/opt/compiler-explorer/gcc-10.2.0/bin/../lib/gcc/x86_64-linux-gnu/10.2.0/
<source> -quiet -dumpbase example.c -masm=intel -mtune=generic -march=x86-64
-auxbase-strip ./output.s -g -O3 -version -fdiagnostics-color=always -o
./output.s
GNU C17 (Compiler-Explorer-Build) version 10.2.0 (x86_64-linux-gnu)
        compiled by GNU C version 7.5.0, GMP version 6.1.0, MPFR version 3.1.4,
MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory
"/opt/compiler-explorer/gcc-10.2.0/bin/../lib/gcc/x86_64-linux-gnu/10.2.0/../../../../x86_64-linux-gnu/include"
ignoring duplicate directory
"/opt/compiler-explorer/gcc-10.2.0/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/10.2.0/include"
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring duplicate directory
"/opt/compiler-explorer/gcc-10.2.0/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/10.2.0/include-fixed"
ignoring nonexistent directory
"/opt/compiler-explorer/gcc-10.2.0/bin/../lib/gcc/../../lib/gcc/x86_64-linux-gnu/10.2.0/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:

/opt/compiler-explorer/gcc-10.2.0/bin/../lib/gcc/x86_64-linux-gnu/10.2.0/include

/opt/compiler-explorer/gcc-10.2.0/bin/../lib/gcc/x86_64-linux-gnu/10.2.0/include-fixed
 /usr/local/include
 /opt/compiler-explorer/gcc-10.2.0/bin/../lib/gcc/../../include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C17 (Compiler-Explorer-Build) version 10.2.0 (x86_64-linux-gnu)
        compiled by GNU C version 7.5.0, GMP version 6.1.0, MPFR version 3.1.4,
MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 870fa0f1a93c5f0ff6fd5ef23d839e5a
COMPILER_PATH=/opt/compiler-explorer/gcc-10.2.0/bin/../libexec/gcc/x86_64-linux-gnu/10.2.0/:/opt/compiler-explorer/gcc-10.2.0/bin/../libexec/gcc/x86_64-linux-gnu/:/opt/compiler-explorer/gcc-10.2.0/bin/../libexec/gcc/:/opt/compiler-explorer/gcc-10.2.0/bin/../lib/gcc/x86_64-linux-gnu/10.2.0/../../../../x86_64-linux-gnu/bin/
LIBRARY_PATH=/opt/compiler-explorer/gcc-10.2.0/bin/../lib/gcc/x86_64-linux-gnu/10.2.0/:/opt/compiler-explorer/gcc-10.2.0/bin/../lib/gcc/x86_64-linux-gnu/:/opt/compiler-explorer/gcc-10.2.0/bin/../lib/gcc/:/opt/compiler-explorer/gcc-10.2.0/bin/../lib/gcc/x86_64-linux-gnu/10.2.0/../../../../lib64/:/lib/x86_64-linux-gnu/:/lib/../lib64/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib64/:/opt/compiler-explorer/gcc-10.2.0/bin/../lib/gcc/x86_64-linux-gnu/10.2.0/../../../../x86_64-linux-gnu/lib/:/opt/compiler-explorer/gcc-10.2.0/bin/../lib/gcc/x86_64-linux-gnu/10.2.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-fdiagnostics-color=always' '-g' '-o' './output.s'
'-masm=intel' '-S' '-v' '-O3' '-mtune=generic' '-march=x86-64'
Compiler returned: 0

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

* [Bug c/99011] Potentially missed optimization. Arrays are created without need
  2021-02-08 19:05 [Bug c/99011] New: Potentially missed optimization. Arrays are created without need pj at hugeone dot co.uk
@ 2021-02-08 19:14 ` pj at hugeone dot co.uk
  2021-02-08 21:08 ` [Bug middle-end/99011] " pinskia at gcc dot gnu.org
  2021-02-09  7:27 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pj at hugeone dot co.uk @ 2021-02-08 19:14 UTC (permalink / raw)
  To: gcc-bugs

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

Piotr <pj at hugeone dot co.uk> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|10.2.1                      |10.2.0

--- Comment #1 from Piotr <pj at hugeone dot co.uk> ---
https://godbolt.org/z/E18djs

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

* [Bug middle-end/99011] Potentially missed optimization. Arrays are created without need
  2021-02-08 19:05 [Bug c/99011] New: Potentially missed optimization. Arrays are created without need pj at hugeone dot co.uk
  2021-02-08 19:14 ` [Bug c/99011] " pj at hugeone dot co.uk
@ 2021-02-08 21:08 ` pinskia at gcc dot gnu.org
  2021-02-09  7:27 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-02-08 21:08 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
          Component|c                           |middle-end
           Keywords|                            |missed-optimization

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

* [Bug middle-end/99011] Potentially missed optimization. Arrays are created without need
  2021-02-08 19:05 [Bug c/99011] New: Potentially missed optimization. Arrays are created without need pj at hugeone dot co.uk
  2021-02-08 19:14 ` [Bug c/99011] " pj at hugeone dot co.uk
  2021-02-08 21:08 ` [Bug middle-end/99011] " pinskia at gcc dot gnu.org
@ 2021-02-09  7:27 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-02-09  7:27 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-02-09
     Ever confirmed|0                           |1

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  The issue is the array accesses remain variable and there's no
transform that turns a two-valued variable access into two (constant folded)
accesses plus a select.  Depending on how expensive it is to load the constants
such transform may not always be profitable.

Given we're looking for a two-valued variable access it might be tempting to
do the transform in VRP where there's also a related PR around deriving
a range for the loaded value from the entries of the indexed array (PR80603).

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

end of thread, other threads:[~2021-02-09  7:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-08 19:05 [Bug c/99011] New: Potentially missed optimization. Arrays are created without need pj at hugeone dot co.uk
2021-02-08 19:14 ` [Bug c/99011] " pj at hugeone dot co.uk
2021-02-08 21:08 ` [Bug middle-end/99011] " pinskia at gcc dot gnu.org
2021-02-09  7:27 ` rguenth 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).