public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/64215] New: -Os misses an opportunity to merge two ret instructions
@ 2014-12-07 23:36 mpeg.blue at free dot fr
  2014-12-08  8:52 ` [Bug target/64215] " jakub at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: mpeg.blue at free dot fr @ 2014-12-07 23:36 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 64215
           Summary: -Os misses an opportunity to merge two ret
                    instructions
           Product: gcc
           Version: 4.8.2
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mpeg.blue at free dot fr

The following code:

int find_mismatch(const char *s, const char *t)
{
  int n;
  for (n = 0; s[n] == t[n]; ++n) if (s[n] == 0) return -1;
  return n;
}

optimized for space, by version 4.8.2-19ubuntu1,
produces the following amd64 assembly code:

0000000000000000 <find_mismatch>:
   0:    31 c0                    xor    %eax,%eax
   2:    8a 14 07                 mov    (%rdi,%rax,1),%dl
   5:    3a 14 06                 cmp    (%rsi,%rax,1),%dl
   8:    75 0b                    jne    15 <find_mismatch+0x15>
   a:    48 ff c0                 inc    %rax
   d:    84 d2                    test   %dl,%dl
   f:    75 f1                    jne    2 <find_mismatch+0x2>
  11:    83 c8 ff                 or     $0xffffffff,%eax
  14:    c3                       retq   
  15:    c3                       retq   

The last two instructions could be merged, saving a grand total of 1 byte.


$ gcc -v -Os -c parse.c
Using built-in specs.
COLLECT_GCC=gcc
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.2-19ubuntu1'
--with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs
--enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.8 --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls
--with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap
--enable-plugin --with-system-zlib --disable-browser-plugin
--enable-java-awt=gtk --enable-gtk-cairo
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home
--with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64
--with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64
--with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar
--enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686
--with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) 
COLLECT_GCC_OPTIONS='-v' '-Os' '-c' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/4.8/cc1 -quiet -v -imultiarch x86_64-linux-gnu
parse.c -quiet -dumpbase parse.c -mtune=generic -march=x86-64 -auxbase parse
-Os -version -fstack-protector -Wformat -Wformat-security -o /tmp/ccZIGWnY.s
GNU C (Ubuntu 4.8.2-19ubuntu1) version 4.8.2 (x86_64-linux-gnu)
    compiled by GNU C version 4.8.2, GMP version 5.1.3, MPFR version 3.1.2-p3,
MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=97 --param ggc-min-heapsize=127216
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C (Ubuntu 4.8.2-19ubuntu1) version 4.8.2 (x86_64-linux-gnu)
    compiled by GNU C version 4.8.2, GMP version 5.1.3, MPFR version 3.1.2-p3,
MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=97 --param ggc-min-heapsize=127216
Compiler executable checksum: dc75e0628c9356affcec059d0c81cc01
COLLECT_GCC_OPTIONS='-v' '-Os' '-c' '-mtune=generic' '-march=x86-64'
 as -v --64 -o parse.o /tmp/ccZIGWnY.s
GNU assembler version 2.24 (x86_64-linux-gnu) using BFD version (GNU Binutils
for Ubuntu) 2.24
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-Os' '-c' '-mtune=generic' '-march=x86-64'


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

* [Bug target/64215] -Os misses an opportunity to merge two ret instructions
  2014-12-07 23:36 [Bug target/64215] New: -Os misses an opportunity to merge two ret instructions mpeg.blue at free dot fr
@ 2014-12-08  8:52 ` jakub at gcc dot gnu.org
  2014-12-08 13:25 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: jakub at gcc dot gnu.org @ 2014-12-08  8:52 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Reproduced with the trunk too.  The problem is that during cross-jumping (the
"jump2" pass), this isn't cheaply cross-jumpable, as the simple_return just
using earlier computed %eax comes before the bb with setting %eax to -1 and
doing simple_return (so in that case it would be replacement of simple_return
with conditional jump, where the conditional jump is larger).  It is only in
the basic block reordering pass that this is swapped, but that is too late, we
don't have any further crossjumping pass.


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

* [Bug target/64215] -Os misses an opportunity to merge two ret instructions
  2014-12-07 23:36 [Bug target/64215] New: -Os misses an opportunity to merge two ret instructions mpeg.blue at free dot fr
  2014-12-08  8:52 ` [Bug target/64215] " jakub at gcc dot gnu.org
@ 2014-12-08 13:25 ` rguenth at gcc dot gnu.org
  2021-08-09  7:36 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-12-08 13:25 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
             Target|                            |x86_64-*-*
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2014-12-08
     Ever confirmed|0                           |1


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

* [Bug target/64215] -Os misses an opportunity to merge two ret instructions
  2014-12-07 23:36 [Bug target/64215] New: -Os misses an opportunity to merge two ret instructions mpeg.blue at free dot fr
  2014-12-08  8:52 ` [Bug target/64215] " jakub at gcc dot gnu.org
  2014-12-08 13:25 ` rguenth at gcc dot gnu.org
@ 2021-08-09  7:36 ` pinskia at gcc dot gnu.org
  2022-09-28 20:50 ` tommy_murphy at hotmail dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-09  7:36 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2014-12-08 00:00:00         |2021-8-9
           Severity|minor                       |enhancement

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

* [Bug target/64215] -Os misses an opportunity to merge two ret instructions
  2014-12-07 23:36 [Bug target/64215] New: -Os misses an opportunity to merge two ret instructions mpeg.blue at free dot fr
                   ` (2 preceding siblings ...)
  2021-08-09  7:36 ` pinskia at gcc dot gnu.org
@ 2022-09-28 20:50 ` tommy_murphy at hotmail dot com
  2022-09-28 21:02 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: tommy_murphy at hotmail dot com @ 2022-09-28 20:50 UTC (permalink / raw)
  To: gcc-bugs

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

Tommy Murphy <tommy_murphy at hotmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tommy_murphy at hotmail dot com

--- Comment #2 from Tommy Murphy <tommy_murphy at hotmail dot com> ---
FWIW this issue was also noticed in the context of the RISC-V GCC toolchain:

https://github.com/riscv-collab/riscv-gnu-toolchain/issues/1048

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

* [Bug target/64215] -Os misses an opportunity to merge two ret instructions
  2014-12-07 23:36 [Bug target/64215] New: -Os misses an opportunity to merge two ret instructions mpeg.blue at free dot fr
                   ` (3 preceding siblings ...)
  2022-09-28 20:50 ` tommy_murphy at hotmail dot com
@ 2022-09-28 21:02 ` pinskia at gcc dot gnu.org
  2022-09-28 22:34 ` tommy_murphy at hotmail dot com
  2023-10-07 19:47 ` law at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-09-28 21:02 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Hmm, I thought this would have been fixed by r13-2871-g1b74b5cb4e9d71 but it
was not ...

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

* [Bug target/64215] -Os misses an opportunity to merge two ret instructions
  2014-12-07 23:36 [Bug target/64215] New: -Os misses an opportunity to merge two ret instructions mpeg.blue at free dot fr
                   ` (4 preceding siblings ...)
  2022-09-28 21:02 ` pinskia at gcc dot gnu.org
@ 2022-09-28 22:34 ` tommy_murphy at hotmail dot com
  2023-10-07 19:47 ` law at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: tommy_murphy at hotmail dot com @ 2022-09-28 22:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Tommy Murphy <tommy_murphy at hotmail dot com> ---
I meant to say that maybe the target needs to be widened/generalised beyond
just "Target:       x86_64-*-*"? As per my previous comment and link, the same
issue has been observed with RISC-V and this comment also mentions it being
observed with AVR and MIPS64:

https://github.com/riscv-collab/riscv-gnu-toolchain/issues/1048#issuecomment-1091943956

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

* [Bug target/64215] -Os misses an opportunity to merge two ret instructions
  2014-12-07 23:36 [Bug target/64215] New: -Os misses an opportunity to merge two ret instructions mpeg.blue at free dot fr
                   ` (5 preceding siblings ...)
  2022-09-28 22:34 ` tommy_murphy at hotmail dot com
@ 2023-10-07 19:47 ` law at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: law at gcc dot gnu.org @ 2023-10-07 19:47 UTC (permalink / raw)
  To: gcc-bugs

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

Jeffrey A. Law <law at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |law at gcc dot gnu.org

--- Comment #5 from Jeffrey A. Law <law at gcc dot gnu.org> ---
Andrew, the reason the patch you referenced doesn't help this case is because
we don't have an unconditional jump to a return only block.

To optimize this case we'd have to detect that we have a return only block that
is immediately preceded by another return block after bbro.

ie:

(note 48 23 59 6 [bb 6] NOTE_INSN_BASIC_BLOCK)
(insn 59 48 49 6 (use (reg/i:SI 10 a0)) -1
     (nil))
(jump_insn 49 59 37 6 (simple_return) 346 {simple_return}
     (nil)
 -> simple_return)
;; lr  out       1 [ra] 2 [sp] 10 [a0]
;; live  out     1 [ra] 2 [sp] 10 [a0]

;;  succ:       EXIT [always]  count:52738306 (estimated locally, freq 0.4591)

;; basic block 7, loop depth 0, count 6317494 (estimated locally, freq 0.0550),
maybe hot
;;  prev block 6, next block 1, flags: (REACHABLE, RTL)
;;  pred:       2 [5.5% (guessed)]  count:6317494 (estimated locally, freq
0.0550) (CAN_FALLTHRU)
;; bb 7 artificial_defs: { }
;; bb 7 artificial_uses: { u-1(2){ }}
;; lr  in        1 [ra] 2 [sp] 10 [a0]
;; lr  use       2 [sp] 10 [a0]
;; lr  def
;; live  in      1 [ra] 2 [sp] 10 [a0]
;; live  gen
;; live  kill

(code_label 37 49 36 7 4 (nil) [1 uses])
(note 36 37 60 7 [bb 7] NOTE_INSN_BASIC_BLOCK)
(insn 60 36 51 7 (use (reg/i:SI 10 a0)) -1
     (nil))
(jump_insn 51 60 41 7 (simple_return) 346 {simple_return}
     (nil)
 -> simple_return)

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

end of thread, other threads:[~2023-10-07 19:47 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-07 23:36 [Bug target/64215] New: -Os misses an opportunity to merge two ret instructions mpeg.blue at free dot fr
2014-12-08  8:52 ` [Bug target/64215] " jakub at gcc dot gnu.org
2014-12-08 13:25 ` rguenth at gcc dot gnu.org
2021-08-09  7:36 ` pinskia at gcc dot gnu.org
2022-09-28 20:50 ` tommy_murphy at hotmail dot com
2022-09-28 21:02 ` pinskia at gcc dot gnu.org
2022-09-28 22:34 ` tommy_murphy at hotmail dot com
2023-10-07 19:47 ` law 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).