public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "klepikov.alex+bugs at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/49263] SH Target: underutilized "TST #imm, R0" instruction
Date: Wed, 24 May 2023 13:34:00 +0000	[thread overview]
Message-ID: <bug-49263-4-PdcUyg9RTJ@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-49263-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #37 from Alexander Klepikov <klepikov.alex+bugs at gmail dot com> ---
> Can you also compile for little endian, and most of all, use -O2
> optimization level.  Some optimizations are not done below -O2.

Here's source file, I added functions with non-constant shifts

$ cat f.c
#define ADDR 0xFFFF0000
#define P ((unsigned char *)ADDR)
#define FLAG 0x40
#define S 7

unsigned char f_char_var(char v){
    return (v & FLAG) == FLAG;
}

unsigned char f_unsigned_char_var(unsigned char v){
    return (v & FLAG) == FLAG;
}

unsigned char f_symbol(void){
    return (*P & FLAG) == FLAG;
}

unsigned char f_symbol_zero(void){
    return (*P & FLAG) == 0;
}

unsigned char f_symbol_non_zero(void){
    return (*P & FLAG) != 0;
}

unsigned int dyn_lshift (unsigned int x, unsigned int y)
{
  return x << (y & 31);
}

unsigned int dyn_rshift (unsigned int x, unsigned int y)
{
  return x >> (y & 31);
}

unsigned int really_dyn_lshift (unsigned int x, unsigned int y)
{
  return x << y;
}

unsigned int really_dyn_rshift (unsigned int x, unsigned int y)
{
  return x >> y;
}

With patch disabled, -O2 -mb:

$ cat f.s
        .file   "f.c"
        .text
        .text
        .align 1
        .align 2
        .global _f_char_var
        .type   _f_char_var, @function
_f_char_var:
        mov.l   .L4,r1
        sts.l   pr,@-r15
        jsr     @r1
        exts.b  r4,r4
        mov     r4,r0
        and     #1,r0
        lds.l   @r15+,pr
        rts
        nop
.L5:
        .align 2
.L4:
        .long   ___ashiftrt_r4_6
        .size   _f_char_var, .-_f_char_var
        .align 1
        .align 2
        .global _f_unsigned_char_var
        .type   _f_unsigned_char_var, @function
_f_unsigned_char_var:
        mov.l   .L8,r1
        sts.l   pr,@-r15
        jsr     @r1
        exts.b  r4,r4
        mov     r4,r0
        and     #1,r0
        lds.l   @r15+,pr
        rts
        nop
.L9:
        .align 2
.L8:
        .long   ___ashiftrt_r4_6
        .size   _f_unsigned_char_var, .-_f_unsigned_char_var
        .align 1
        .align 2
        .global _f_symbol
        .type   _f_symbol, @function
_f_symbol:
        mov.l   .L12,r1
        sts.l   pr,@-r15
        mov.b   @r1,r4
        mov.l   .L13,r1
        jsr     @r1
        nop
        mov     r4,r0
        and     #1,r0
        lds.l   @r15+,pr
        rts
        nop
.L14:
        .align 2
.L12:
        .long   -65536
.L13:
        .long   ___ashiftrt_r4_6
        .size   _f_symbol, .-_f_symbol
        .align 1
        .align 2
        .global _f_symbol_zero
        .type   _f_symbol_zero, @function
_f_symbol_zero:
        mov.l   .L16,r1
        mov.b   @r1,r0
        tst     #64,r0
        rts
        movt    r0
.L17:
        .align 2
.L16:
        .long   -65536
        .size   _f_symbol_zero, .-_f_symbol_zero
        .align 1
        .align 2
        .global _f_symbol_non_zero
        .type   _f_symbol_non_zero, @function
_f_symbol_non_zero:
        mov.l   .L20,r1
        sts.l   pr,@-r15
        mov.b   @r1,r4
        mov.l   .L21,r1
        jsr     @r1
        nop
        mov     r4,r0
        and     #1,r0
        lds.l   @r15+,pr
        rts
        nop
.L22:
        .align 2
.L20:
        .long   -65536
.L21:
        .long   ___ashiftrt_r4_6
        .size   _f_symbol_non_zero, .-_f_symbol_non_zero
        .align 1
        .align 2
        .global _dyn_lshift
        .type   _dyn_lshift, @function
_dyn_lshift:
        mov.l   .L25,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L26:
        .align 2
.L25:
        .long   ___ashlsi3_r0
        .size   _dyn_lshift, .-_dyn_lshift
        .align 1
        .align 2
        .global _dyn_rshift
        .type   _dyn_rshift, @function
_dyn_rshift:
        mov.l   .L29,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L30:
        .align 2
.L29:
        .long   ___lshrsi3_r0
        .size   _dyn_rshift, .-_dyn_rshift
        .align 1
        .align 2
        .global _really_dyn_lshift
        .type   _really_dyn_lshift, @function
_really_dyn_lshift:
        mov.l   .L33,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L34:
        .align 2
.L33:
        .long   ___ashlsi3_r0
        .size   _really_dyn_lshift, .-_really_dyn_lshift
        .align 1
        .align 2
        .global _really_dyn_rshift
        .type   _really_dyn_rshift, @function
_really_dyn_rshift:
        mov.l   .L37,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L38:
        .align 2
.L37:
        .long   ___lshrsi3_r0
        .size   _really_dyn_rshift, .-_really_dyn_rshift
        .ident  "GCC: (GNU) 12.3.0"

With patch disabled, -O2 -ml

$ cat f.s
        .file   "f.c"
        .text
        .little
        .text
        .align 1
        .align 2
        .global _f_char_var
        .type   _f_char_var, @function
_f_char_var:
        mov.l   .L4,r1
        sts.l   pr,@-r15
        jsr     @r1
        exts.b  r4,r4
        mov     r4,r0
        and     #1,r0
        lds.l   @r15+,pr
        rts
        nop
.L5:
        .align 2
.L4:
        .long   ___ashiftrt_r4_6
        .size   _f_char_var, .-_f_char_var
        .align 1
        .align 2
        .global _f_unsigned_char_var
        .type   _f_unsigned_char_var, @function
_f_unsigned_char_var:
        mov.l   .L8,r1
        sts.l   pr,@-r15
        jsr     @r1
        exts.b  r4,r4
        mov     r4,r0
        and     #1,r0
        lds.l   @r15+,pr
        rts
        nop
.L9:
        .align 2
.L8:
        .long   ___ashiftrt_r4_6
        .size   _f_unsigned_char_var, .-_f_unsigned_char_var
        .align 1
        .align 2
        .global _f_symbol
        .type   _f_symbol, @function
_f_symbol:
        mov.l   .L12,r1
        sts.l   pr,@-r15
        mov.b   @r1,r4
        mov.l   .L13,r1
        jsr     @r1
        nop
        mov     r4,r0
        and     #1,r0
        lds.l   @r15+,pr
        rts
        nop
.L14:
        .align 2
.L12:
        .long   -65536
.L13:
        .long   ___ashiftrt_r4_6
        .size   _f_symbol, .-_f_symbol
        .align 1
        .align 2
        .global _f_symbol_zero
        .type   _f_symbol_zero, @function
_f_symbol_zero:
        mov.l   .L16,r1
        mov.b   @r1,r0
        tst     #64,r0
        rts
        movt    r0
.L17:
        .align 2
.L16:
        .long   -65536
        .size   _f_symbol_zero, .-_f_symbol_zero
        .align 1
        .align 2
        .global _f_symbol_non_zero
        .type   _f_symbol_non_zero, @function
_f_symbol_non_zero:
        mov.l   .L20,r1
        sts.l   pr,@-r15
        mov.b   @r1,r4
        mov.l   .L21,r1
        jsr     @r1
        nop
        mov     r4,r0
        and     #1,r0
        lds.l   @r15+,pr
        rts
        nop
.L22:
        .align 2
.L20:
        .long   -65536
.L21:
        .long   ___ashiftrt_r4_6
        .size   _f_symbol_non_zero, .-_f_symbol_non_zero
        .align 1
        .align 2
        .global _dyn_lshift
        .type   _dyn_lshift, @function
_dyn_lshift:
        mov.l   .L25,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L26:
        .align 2
.L25:
        .long   ___ashlsi3_r0
        .size   _dyn_lshift, .-_dyn_lshift
        .align 1
        .align 2
        .global _dyn_rshift
        .type   _dyn_rshift, @function
_dyn_rshift:
        mov.l   .L29,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L30:
        .align 2
.L29:
        .long   ___lshrsi3_r0
        .size   _dyn_rshift, .-_dyn_rshift
        .align 1
        .align 2
        .global _really_dyn_lshift
        .type   _really_dyn_lshift, @function
_really_dyn_lshift:
        mov.l   .L33,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L34:
        .align 2
.L33:
        .long   ___ashlsi3_r0
        .size   _really_dyn_lshift, .-_really_dyn_lshift
        .align 1
        .align 2
        .global _really_dyn_rshift
        .type   _really_dyn_rshift, @function
_really_dyn_rshift:
        mov.l   .L37,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L38:
        .align 2
.L37:
        .long   ___lshrsi3_r0
        .size   _really_dyn_rshift, .-_really_dyn_rshift
        .ident  "GCC: (GNU) 12.3.0"

With patch enabled -O2 -mb

$ cat f.s
        .file   "f.c"
        .text
        .text
        .align 1
        .align 2
        .global _f_char_var
        .type   _f_char_var, @function
_f_char_var:
        mov     r4,r0
        tst     #64,r0
        mov     #-1,r0
        rts
        negc    r0,r0
        .size   _f_char_var, .-_f_char_var
        .align 1
        .align 2
        .global _f_unsigned_char_var
        .type   _f_unsigned_char_var, @function
_f_unsigned_char_var:
        mov     r4,r0
        tst     #64,r0
        mov     #-1,r0
        rts
        negc    r0,r0
        .size   _f_unsigned_char_var, .-_f_unsigned_char_var
        .align 1
        .align 2
        .global _f_symbol
        .type   _f_symbol, @function
_f_symbol:
        mov.l   .L5,r1
        mov.b   @r1,r0
        tst     #64,r0
        mov     #-1,r0
        rts
        negc    r0,r0
.L6:
        .align 2
.L5:
        .long   -65536
        .size   _f_symbol, .-_f_symbol
        .align 1
        .align 2
        .global _f_symbol_zero
        .type   _f_symbol_zero, @function
_f_symbol_zero:
        mov.l   .L8,r1
        mov.b   @r1,r0
        tst     #64,r0
        rts
        movt    r0
.L9:
        .align 2
.L8:
        .long   -65536
        .size   _f_symbol_zero, .-_f_symbol_zero
        .align 1
        .align 2
        .global _f_symbol_non_zero
        .type   _f_symbol_non_zero, @function
_f_symbol_non_zero:
        mov.l   .L11,r1
        mov.b   @r1,r0
        tst     #64,r0
        mov     #-1,r0
        rts
        negc    r0,r0
.L12:
        .align 2
.L11:
        .long   -65536
        .size   _f_symbol_non_zero, .-_f_symbol_non_zero
        .align 1
        .align 2
        .global _dyn_lshift
        .type   _dyn_lshift, @function
_dyn_lshift:
        mov.l   .L15,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L16:
        .align 2
.L15:
        .long   ___ashlsi3_r0
        .size   _dyn_lshift, .-_dyn_lshift
        .align 1
        .align 2
        .global _dyn_rshift
        .type   _dyn_rshift, @function
_dyn_rshift:
        mov.l   .L19,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L20:
        .align 2
.L19:
        .long   ___lshrsi3_r0
        .size   _dyn_rshift, .-_dyn_rshift
        .align 1
        .align 2
        .global _really_dyn_lshift
        .type   _really_dyn_lshift, @function
_really_dyn_lshift:
        mov.l   .L23,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L24:
        .align 2
.L23:
        .long   ___ashlsi3_r0
        .size   _really_dyn_lshift, .-_really_dyn_lshift
        .align 1
        .align 2
        .global _really_dyn_rshift
        .type   _really_dyn_rshift, @function
_really_dyn_rshift:
        mov.l   .L27,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L28:
        .align 2
.L27:
        .long   ___lshrsi3_r0
        .size   _really_dyn_rshift, .-_really_dyn_rshift
        .ident  "GCC: (GNU) 12.3.0"

With patch enabled, -O2 -ml

$ cat f.s
        .file   "f.c"
        .text
        .little
        .text
        .align 1
        .align 2
        .global _f_char_var
        .type   _f_char_var, @function
_f_char_var:
        mov     r4,r0
        tst     #64,r0
        mov     #-1,r0
        rts
        negc    r0,r0
        .size   _f_char_var, .-_f_char_var
        .align 1
        .align 2
        .global _f_unsigned_char_var
        .type   _f_unsigned_char_var, @function
_f_unsigned_char_var:
        mov     r4,r0
        tst     #64,r0
        mov     #-1,r0
        rts
        negc    r0,r0
        .size   _f_unsigned_char_var, .-_f_unsigned_char_var
        .align 1
        .align 2
        .global _f_symbol
        .type   _f_symbol, @function
_f_symbol:
        mov.l   .L5,r1
        mov.b   @r1,r0
        tst     #64,r0
        mov     #-1,r0
        rts
        negc    r0,r0
.L6:
        .align 2
.L5:
        .long   -65536
        .size   _f_symbol, .-_f_symbol
        .align 1
        .align 2
        .global _f_symbol_zero
        .type   _f_symbol_zero, @function
_f_symbol_zero:
        mov.l   .L8,r1
        mov.b   @r1,r0
        tst     #64,r0
        rts
        movt    r0
.L9:
        .align 2
.L8:
        .long   -65536
        .size   _f_symbol_zero, .-_f_symbol_zero
        .align 1
        .align 2
        .global _f_symbol_non_zero
        .type   _f_symbol_non_zero, @function
_f_symbol_non_zero:
        mov.l   .L11,r1
        mov.b   @r1,r0
        tst     #64,r0
        mov     #-1,r0
        rts
        negc    r0,r0
.L12:
        .align 2
.L11:
        .long   -65536
        .size   _f_symbol_non_zero, .-_f_symbol_non_zero
        .align 1
        .align 2
        .global _dyn_lshift
        .type   _dyn_lshift, @function
_dyn_lshift:
        mov.l   .L15,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L16:
        .align 2
.L15:
        .long   ___ashlsi3_r0
        .size   _dyn_lshift, .-_dyn_lshift
        .align 1
        .align 2
        .global _dyn_rshift
        .type   _dyn_rshift, @function
_dyn_rshift:
        mov.l   .L19,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L20:
        .align 2
.L19:
        .long   ___lshrsi3_r0
        .size   _dyn_rshift, .-_dyn_rshift
        .align 1
        .align 2
        .global _really_dyn_lshift
        .type   _really_dyn_lshift, @function
_really_dyn_lshift:
        mov.l   .L23,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L24:
        .align 2
.L23:
        .long   ___ashlsi3_r0
        .size   _really_dyn_lshift, .-_really_dyn_lshift
        .align 1
        .align 2
        .global _really_dyn_rshift
        .type   _really_dyn_rshift, @function
_really_dyn_rshift:
        mov.l   .L27,r1
        sts.l   pr,@-r15
        jsr     @r1
        mov     r5,r0
        lds.l   @r15+,pr
        rts
        nop
.L28:
        .align 2
.L27:
        .long   ___lshrsi3_r0
        .size   _really_dyn_rshift, .-_really_dyn_rshift
        .ident  "GCC: (GNU) 12.3.0" 

> '-mdisable-dynshift-libcall' would be more appropriate for what it tries to
> do, I think.  Although that is a whole different issue ... but what is it
> going to do for real dynamic shifts on SH2?
> 
> What kind of code is it supposed to emit for things like
> 
> unsigned int dyn_shift (unsigned int x, unsigned int y)
> {
>   return x << (y & 31);
> }

As far as I understand from GCC sources, function I patched 'expand_ashiftrt'
process only constant values of shift. As you can see earlier, I added your and
other examples to tests. It looks like really dynamic shifts translate to
library calls.

Should I test more exotic situations? If so, could you please help me with
really exotic or weired examples?

  parent reply	other threads:[~2023-05-24 13:34 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-01 20:17 [Bug target/49263] New: " oleg.endo@t-online.de
2011-06-01 20:42 ` [Bug target/49263] " oleg.endo@t-online.de
2011-06-12 23:12 ` kkojima at gcc dot gnu.org
2011-06-19 16:42 ` oleg.endo@t-online.de
2011-06-22 22:34 ` kkojima at gcc dot gnu.org
2011-06-26 22:31 ` oleg.endo@t-online.de
2011-06-27  5:15 ` kkojima at gcc dot gnu.org
2011-10-09 23:35 ` oleg.endo@t-online.de
2011-10-10  1:32 ` kkojima at gcc dot gnu.org
2011-10-10 23:48 ` oleg.endo@t-online.de
2011-10-11  1:47 ` kkojima at gcc dot gnu.org
2011-10-13 22:55 ` oleg.endo@t-online.de
2011-10-14 23:06 ` kkojima at gcc dot gnu.org
2011-10-15  2:33 ` kkojima at gcc dot gnu.org
2011-11-20 14:20 ` oleg.endo@t-online.de
2011-12-29  1:09 ` oleg.endo@t-online.de
2012-02-26 16:28 ` olegendo at gcc dot gnu.org
2012-02-26 23:29 ` olegendo at gcc dot gnu.org
2012-08-27 19:52 ` olegendo at gcc dot gnu.org
2012-10-28 22:02 ` olegendo at gcc dot gnu.org
2012-10-31 13:47 ` olegendo at gcc dot gnu.org
2013-12-08 13:47 ` olegendo at gcc dot gnu.org
2013-12-17 12:37 ` olegendo at gcc dot gnu.org
2014-12-30 18:45 ` olegendo at gcc dot gnu.org
2015-01-24 13:05 ` olegendo at gcc dot gnu.org
2015-01-26 23:57 ` olegendo at gcc dot gnu.org
2023-05-12 11:46 ` klepikov.alex+bugs at gmail dot com
2023-05-23 12:34 ` klepikov.alex+bugs at gmail dot com
2023-05-23 12:35 ` klepikov.alex+bugs at gmail dot com
2023-05-23 19:05 ` olegendo at gcc dot gnu.org
2023-05-24 11:40 ` klepikov.alex+bugs at gmail dot com
2023-05-24 11:57 ` olegendo at gcc dot gnu.org
2023-05-24 13:34 ` klepikov.alex+bugs at gmail dot com [this message]
2023-05-24 15:00 ` olegendo at gcc dot gnu.org
2023-05-25 17:53 ` klepikov.alex+bugs at gmail dot com
2023-05-25 21:32 ` olegendo at gcc dot gnu.org
2023-05-26 12:03 ` klepikov.alex+bugs at gmail dot com
2023-05-26 17:44 ` olegendo at gcc dot gnu.org
2023-05-28 10:24 ` klepikov.alex+bugs at gmail dot com
2023-05-28 10:48 ` olegendo at gcc dot gnu.org
2023-05-29 14:54 ` klepikov.alex+bugs at gmail dot com
2023-05-30  1:48 ` egallager at gcc dot gnu.org
2023-05-30  1:56 ` olegendo at gcc dot gnu.org
2023-05-30 12:42 ` klepikov.alex+bugs at gmail dot com
2023-05-30 19:57 ` olegendo at gcc dot gnu.org
2023-05-30 20:00 ` olegendo 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-49263-4-PdcUyg9RTJ@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: link
Be 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).