Bug 99988 - aarch64: GCC generates excessive consecutive bti j instructions
Summary: aarch64: GCC generates excessive consecutive bti j instructions
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 11.0
: P3 normal
Target Milestone: ---
Assignee: Alex Coplan
URL: https://gcc.gnu.org/pipermail/gcc-pat...
Keywords: missed-optimization, patch
Depends on:
Blocks:
 
Reported: 2021-04-09 10:13 UTC by Alex Coplan
Modified: 2021-05-12 15:08 UTC (History)
0 users

See Also:
Host:
Target: aarch64
Build:
Known to work:
Known to fail: 10.3.1, 11.0, 9.3.1
Last reconfirmed: 2021-04-09 00:00:00


Attachments
minimal reproducer (194 bytes, text/plain)
2021-04-09 10:13 UTC, Alex Coplan
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Alex Coplan 2021-04-09 10:13:05 UTC
Created attachment 50535 [details]
minimal reproducer

For the attached testcase (reduced from the linux kernel), GCC generates multiple redundant sequences of back-to-back bti j instructions, the longest of which is 262 instructions long.

To reproduce:

$ aarch64-linux-gnu-gcc -c test.c -S -o - -O2 -mbranch-protection=standard | uniq -c | grep "bti j" | sort -nr
    262         hint    36 // bti j
      7         hint    36 // bti j
      6         hint    36 // bti j
      4         hint    36 // bti j
      4         hint    36 // bti j
      3         hint    36 // bti j
      2         hint    36 // bti j
      2         hint    36 // bti j
      2         hint    36 // bti j
      2         hint    36 // bti j
      2         hint    36 // bti j
      2         hint    36 // bti j
      2         hint    36 // bti j
Comment 1 Alex Coplan 2021-04-09 10:23:25 UTC
GCC 10 and 9 are also affected.
Comment 2 Alex Coplan 2021-04-09 11:29:32 UTC
Taking a look at this.
Comment 3 GCC Commits 2021-04-21 13:42:59 UTC
The master branch has been updated by Alex Coplan <acoplan@gcc.gnu.org>:

https://gcc.gnu.org/g:fe11882ae34c49f6214f93867783ed1332f35f0f

commit r12-34-gfe11882ae34c49f6214f93867783ed1332f35f0f
Author: Alex Coplan <alex.coplan@arm.com>
Date:   Wed Apr 21 14:42:04 2021 +0100

    aarch64: Avoid duplicating bti j insns for jump tables [PR99988]
    
    This patch fixes PR99988 which shows us generating large (> 250)
    sequences of back-to-back bti j instructions.
    
    The fix is simply to avoid inserting bti j instructions at the target of
    a jump table if we've already inserted one for a given label.
    
    gcc/ChangeLog:
    
            PR target/99988
            * config/aarch64/aarch64-bti-insert.c (aarch64_bti_j_insn_p): New.
            (rest_of_insert_bti): Avoid inserting duplicate bti j insns for
            jump table targets.
    
    gcc/testsuite/ChangeLog:
    
            PR target/99988
            * gcc.target/aarch64/pr99988.c: New test.
Comment 4 Alex Coplan 2021-04-21 13:46:56 UTC
Fixed on trunk. Keeping open for backports (once it's had some decent baking time).
Comment 5 GCC Commits 2021-04-22 12:46:34 UTC
The master branch has been updated by Christophe Lyon <clyon@gcc.gnu.org>:

https://gcc.gnu.org/g:d44d8ed6b4e6fb2d0cf7566214e2e8281dd44cf0

commit r12-58-gd44d8ed6b4e6fb2d0cf7566214e2e8281dd44cf0
Author: Christophe Lyon <christophe.lyon@linaro.org>
Date:   Thu Apr 22 12:43:25 2021 +0000

    testsuite/aarch64: Run pr99988.c test under lp64 only
    
    The new test fails with -mabi=ilp32:
    sorry, unimplemented: return address signing is only supported for '-mabi=lp64'
    
    2021-04-22  Christophe Lyon  <christophe.lyon@linaro.org>
    
            gcc/testsuite/
            PR target/99988
            * gcc.target/aarch64/pr99988.c: Skip if not lp64 target.
Comment 6 GCC Commits 2021-05-11 15:15:58 UTC
The releases/gcc-11 branch has been updated by Alex Coplan <acoplan@gcc.gnu.org>:

https://gcc.gnu.org/g:1011bb8bc67cf34c45a9237d72a801a14ad9ef0f

commit r11-8379-g1011bb8bc67cf34c45a9237d72a801a14ad9ef0f
Author: Alex Coplan <alex.coplan@arm.com>
Date:   Wed Apr 21 14:42:04 2021 +0100

    aarch64: Avoid duplicating bti j insns for jump tables [PR99988]
    
    This patch fixes PR99988 which shows us generating large (> 250)
    sequences of back-to-back bti j instructions.
    
    The fix is simply to avoid inserting bti j instructions at the target of
    a jump table if we've already inserted one for a given label.
    
    Co-Authored-By: Christophe Lyon <clyon@gcc.gnu.org>
    
    gcc/ChangeLog:
    
            PR target/99988
            * config/aarch64/aarch64-bti-insert.c (aarch64_bti_j_insn_p): New.
            (rest_of_insert_bti): Avoid inserting duplicate bti j insns for
            jump table targets.
    
    gcc/testsuite/ChangeLog:
    
            PR target/99988
            * gcc.target/aarch64/pr99988.c: New test.
Comment 7 GCC Commits 2021-05-12 09:58:15 UTC
The releases/gcc-10 branch has been updated by Alex Coplan <acoplan@gcc.gnu.org>:

https://gcc.gnu.org/g:419c243dfb9aba51aceb1370a241db082847eeb5

commit r10-9818-g419c243dfb9aba51aceb1370a241db082847eeb5
Author: Alex Coplan <alex.coplan@arm.com>
Date:   Wed Apr 21 14:42:04 2021 +0100

    aarch64: Avoid duplicating bti j insns for jump tables [PR99988]
    
    This patch fixes PR99988 which shows us generating large (> 250)
    sequences of back-to-back bti j instructions.
    
    The fix is simply to avoid inserting bti j instructions at the target of
    a jump table if we've already inserted one for a given label.
    
    Co-Authored-By: Christophe Lyon <clyon@gcc.gnu.org>
    
    gcc/ChangeLog:
    
            PR target/99988
            * config/aarch64/aarch64-bti-insert.c (aarch64_bti_j_insn_p): New.
            (rest_of_insert_bti): Avoid inserting duplicate bti j insns for
            jump table targets.
    
    gcc/testsuite/ChangeLog:
    
            PR target/99988
            * gcc.target/aarch64/pr99988.c: New test.
    
    (cherry picked from commit 1011bb8bc67cf34c45a9237d72a801a14ad9ef0f)
Comment 8 GCC Commits 2021-05-12 15:03:13 UTC
The releases/gcc-9 branch has been updated by Alex Coplan <acoplan@gcc.gnu.org>:

https://gcc.gnu.org/g:52a09050fdfafb0e41d56b6adf1625264f315b82

commit r9-9528-g52a09050fdfafb0e41d56b6adf1625264f315b82
Author: Alex Coplan <alex.coplan@arm.com>
Date:   Wed Apr 21 14:42:04 2021 +0100

    aarch64: Avoid duplicating bti j insns for jump tables [PR99988]
    
    This patch fixes PR99988 which shows us generating large (> 250)
    sequences of back-to-back bti j instructions.
    
    The fix is simply to avoid inserting bti j instructions at the target of
    a jump table if we've already inserted one for a given label.
    
    Co-Authored-By: Christophe Lyon <clyon@gcc.gnu.org>
    
    gcc/ChangeLog:
    
            PR target/99988
            * config/aarch64/aarch64-bti-insert.c (aarch64_bti_j_insn_p): New.
            (rest_of_insert_bti): Avoid inserting duplicate bti j insns for
            jump table targets.
    
    gcc/testsuite/ChangeLog:
    
            PR target/99988
            * gcc.target/aarch64/pr99988.c: New test.
    
    (cherry picked from commit 1011bb8bc67cf34c45a9237d72a801a14ad9ef0f)
Comment 9 Alex Coplan 2021-05-12 15:08:42 UTC
Fixed everywhere.