public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/110217] New: [avr] SREG: use BSET and BCLR instead of load/modify/write
@ 2023-06-12  9:23 mx682x at gmail dot com
  2023-06-30 16:53 ` [Bug target/110217] " gjl at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: mx682x at gmail dot com @ 2023-06-12  9:23 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110217
           Summary: [avr] SREG: use BSET and BCLR instead of
                    load/modify/write
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mx682x at gmail dot com
  Target Milestone: ---

Created attachment 55306
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55306&action=edit
patch file for gcc 13.1

Hello.

I'd like to suggest a patch that makes the compiler use bset and bclr
instructions when doing SREG &= 0x80 (or ~0x80) or similr. Right now, all
versions, as far as I know, are using in/andi(ori)/out instructions to change
the SREG.

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

* [Bug target/110217] [avr] SREG: use BSET and BCLR instead of load/modify/write
  2023-06-12  9:23 [Bug target/110217] New: [avr] SREG: use BSET and BCLR instead of load/modify/write mx682x at gmail dot com
@ 2023-06-30 16:53 ` gjl at gcc dot gnu.org
  2023-07-10  7:18 ` mx682x at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: gjl at gcc dot gnu.org @ 2023-06-30 16:53 UTC (permalink / raw)
  To: gcc-bugs

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

Georg-Johann Lay <gjl at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

--- Comment #1 from Georg-Johann Lay <gjl at gcc dot gnu.org> ---
The only case where this might make sense is for bit 7 (the I-flag), however
the established coding style is to use cli() and sei() from AVR-LibC, cf.
documentation of #include <avr/interrupt.h>:

https://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html

For more sophitsticated use cases there is even ATOMIC_BLOCK and friends
provided by #include <util/atomic.h>, cf:

https://www.nongnu.org/avr-libc/user-manual/group__util__atomic.html

This has the additional benefit of being more readable than bit manipulations.  

Apart from that, the proposed patch won't work for indirect addressing, or when
the compiler is turning direct addresses to indirect addresses (using CSE etc,
common subexpression elimination and similar strategies).

Also the patch relies on insn combine which only runs when optimization is on,
thus any application which relies on that optimization will glitch at -O0.

So I am inclined to "won't fix" this PR.

Maybe you just missed avr/interrupt.h and / or util/atomic.h ?

If you must not use AVR-LibC for some reason, then the next best approach is to
use __builtin_avr_sei(), cf. AVR built-in functions:

https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/AVR-Built-in-Functions.html

Or implement it as static inline function that does __asm volatile ("sei" :::
"memory") if you are not allowed to use built-ins.

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

* [Bug target/110217] [avr] SREG: use BSET and BCLR instead of load/modify/write
  2023-06-12  9:23 [Bug target/110217] New: [avr] SREG: use BSET and BCLR instead of load/modify/write mx682x at gmail dot com
  2023-06-30 16:53 ` [Bug target/110217] " gjl at gcc dot gnu.org
@ 2023-07-10  7:18 ` mx682x at gmail dot com
  2023-07-12  9:23 ` gjl at gcc dot gnu.org
  2023-07-12 20:01 ` gjl at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: mx682x at gmail dot com @ 2023-07-10  7:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from mx682x at gmail dot com ---
I see, thank you for you input.

> Apart from that, the proposed patch won't work for indirect addressing, or
> when the compiler is turning direct addresses to indirect addresses (using
> CSE etc, common subexpression elimination and similar strategies).
> 
> Also the patch relies on insn combine which only runs when optimization is
> on, thus any application which relies on that optimization will glitch at
> -O0.

However, just out of curiosity, doesn't this also apply to the "Single-Cycle
I/O access" instructions cbi/sbi? Afterall, I've just duplicated the respective
code and andjusted the predicate for the address.

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

* [Bug target/110217] [avr] SREG: use BSET and BCLR instead of load/modify/write
  2023-06-12  9:23 [Bug target/110217] New: [avr] SREG: use BSET and BCLR instead of load/modify/write mx682x at gmail dot com
  2023-06-30 16:53 ` [Bug target/110217] " gjl at gcc dot gnu.org
  2023-07-10  7:18 ` mx682x at gmail dot com
@ 2023-07-12  9:23 ` gjl at gcc dot gnu.org
  2023-07-12 20:01 ` gjl at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: gjl at gcc dot gnu.org @ 2023-07-12  9:23 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Georg-Johann Lay <gjl at gcc dot gnu.org> ---
At least CLI / SBI won't glitch at O0, same for ATOMIC_BLOCK.

For SBI et al. you are right, they rely on insn combine. The right approach
would have been compiler built-ins or API using inline asm. But the time to
introduce such interfaces was 20 or so years ago...

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

* [Bug target/110217] [avr] SREG: use BSET and BCLR instead of load/modify/write
  2023-06-12  9:23 [Bug target/110217] New: [avr] SREG: use BSET and BCLR instead of load/modify/write mx682x at gmail dot com
                   ` (2 preceding siblings ...)
  2023-07-12  9:23 ` gjl at gcc dot gnu.org
@ 2023-07-12 20:01 ` gjl at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: gjl at gcc dot gnu.org @ 2023-07-12 20:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Georg-Johann Lay <gjl at gcc dot gnu.org> ---
At least CLI / SBI won't glitch at O0, same for ATOMIC_BLOCK.

For SBI et al. you are right, they rely on insn combine. The right approach
would have been compiler built-ins or API using inline asm. But the time to
introduce such interfaces was 20 or so years ago...

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

end of thread, other threads:[~2023-07-12 20:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-12  9:23 [Bug target/110217] New: [avr] SREG: use BSET and BCLR instead of load/modify/write mx682x at gmail dot com
2023-06-30 16:53 ` [Bug target/110217] " gjl at gcc dot gnu.org
2023-07-10  7:18 ` mx682x at gmail dot com
2023-07-12  9:23 ` gjl at gcc dot gnu.org
2023-07-12 20:01 ` gjl 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).