public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "gjl at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/109907] New: [avr] Missed optimization for bit extraction (uses shift instead of single bit-test) Date: Fri, 19 May 2023 10:10:10 +0000 [thread overview] Message-ID: <bug-109907-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109907 Bug ID: 109907 Summary: [avr] Missed optimization for bit extraction (uses shift instead of single bit-test) Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: gjl at gcc dot gnu.org Target Milestone: --- Created attachment 55116 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55116&action=edit C test case. The following missed optimization occurs with current v14 master and also with older versions of the compiler: $ avr-gcc ext.c -dumpbase "" -save-temps -dp -mmcu=atmega128 -c -Os Functons like uint8_t cset_32bit31 (uint32_t x) { return (x & (1ul << 31)) ? 1 : 0; // bloat } that extract a single bit might generate very expensive code like: cset_32bit31: movw r26,r24 ; 18 [c=4 l=1] *movhi/0 movw r24,r22 ; 19 [c=4 l=1] *movhi/0 lsl r27 ; 24 [c=16 l=4] *ashrsi3_const/3 sbc r24,r24 mov r25,r24 movw r26,r24 andi r24,lo8(1) ; 12 [c=4 l=1] andqi3/1 ret ; 22 [c=0 l=1] return where the following 3 instructions would suffice. This is smaller, faster and imposes no additioal register pressure: bst r25,7 ; 16 [c=4 l=3] *extzv/4 clr r24 bld r24,0 What also would work is loading 0 or 1 depending on a single bit like: LDI r24, 0 # R24 = 0 SBRC r25, 7 # Skip next instruction if R25.7 == 0. LDI r24, 1 # R24 = 1 The bloat also occurs when the complement of the bit is extracted like in uint8_t cset_32bit30_not (uint32_t x) { return (x & (1ul << 30)) ? 0 : 1; // bloat } cset_32bit30_not: movw r26,r24 ; 19 [c=4 l=1] *movhi/0 movw r24,r22 ; 20 [c=4 l=1] *movhi/0 ldi r18,30 ; 25 [c=44 l=7] *lshrsi3_const/3 1: lsr r27 ror r26 ror r25 ror r24 dec r18 brne 1b ldi r18,1 ; 7 [c=32 l=2] xorsi3/2 eor r24,r18 andi r24,lo8(1) ; 13 [c=4 l=1] andqi3/1 ret ; 23 [c=0 l=1] return This case is even worse because it's a loop of 30 single bit-shifts to extract the bit. Again, skipping one instrauction depending on a bit was possible: LDI r24, 1 # R24 = 1 SBRC r25, 6 # Skip next instruction if R25.7 == 0. LDI r24, 0 # R24 = 0 or LDI r24, 0 # R24 = 0 SBRS r25, 6 # Skip next instruction if R25.7 == 1. LDI r24, 1 # R24 = 1 or extract one bit using the T-flag: BST r25, 6 # SREG.T = R25.6 LDI r24, 0xff # R24 = 0xff BLD r24, 0 # R24.0 = SREG.T COM r24 # R24 = R24 ^ 0xff ------------------------------------------------------- Configured with: --target=avr --disable-nls --with-dwarf2 --with-gnu-as --with-gnu-ld --disable-shared --enable-languages=c,c++ gcc version 14.0.0 20230518 (experimental) (GCC)
next reply other threads:[~2023-05-19 10:10 UTC|newest] Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-05-19 10:10 gjl at gcc dot gnu.org [this message] 2023-05-19 14:14 ` [Bug middle-end/109907] " pinskia at gcc dot gnu.org 2023-05-19 15:42 ` [Bug middle-end/109907] " pinskia at gcc dot gnu.org 2023-05-19 15:45 ` pinskia at gcc dot gnu.org 2023-05-19 20:03 ` pinskia at gcc dot gnu.org 2023-05-19 20:12 ` pinskia at gcc dot gnu.org 2023-05-19 20:41 ` gjl at gcc dot gnu.org 2023-05-19 20:55 ` pinskia at gcc dot gnu.org 2023-05-19 20:59 ` gjl at gcc dot gnu.org 2023-05-20 0:46 ` pinskia at gcc dot gnu.org 2023-05-20 5:09 ` pinskia at gcc dot gnu.org 2023-05-20 7:36 ` gjl at gcc dot gnu.org 2023-05-20 7:50 ` gjl at gcc dot gnu.org 2023-05-20 22:23 ` pinskia at gcc dot gnu.org 2023-05-20 22:40 ` pinskia at gcc dot gnu.org 2023-05-21 5:15 ` pinskia at gcc dot gnu.org 2023-05-21 5:36 ` pinskia at gcc dot gnu.org 2023-05-21 9:09 ` gjl at gcc dot gnu.org 2023-05-21 9:55 ` gjl at gcc dot gnu.org 2023-05-23 9:58 ` gjl at gcc dot gnu.org 2023-05-26 8:49 ` gjl at gcc dot gnu.org 2023-05-26 11:14 ` gjl at gcc dot gnu.org 2023-05-26 15:21 ` pinskia at gcc dot gnu.org 2023-05-26 16:08 ` gjl at gcc dot gnu.org 2023-05-26 23:11 ` pinskia at gcc dot gnu.org 2023-05-26 23:34 ` pinskia at gcc dot gnu.org 2023-05-26 23:49 ` pinskia at gcc dot gnu.org 2023-05-27 0:36 ` pinskia at gcc dot gnu.org 2023-05-27 4:02 ` pinskia at gcc dot gnu.org 2023-05-27 20:09 ` pinskia at gcc dot gnu.org 2023-06-11 9:22 ` cvs-commit at gcc dot gnu.org 2023-06-11 9:26 ` gjl at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-109907-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).