public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/106231] New: sign-extension of the result of `__builtin_tzcnt()`
@ 2022-07-08  8:55 lh_mouse at 126 dot com
  2022-07-08 19:56 ` [Bug middle-end/106231] sign-extension of the result of `__builtin_ctz` pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: lh_mouse at 126 dot com @ 2022-07-08  8:55 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106231
           Summary: sign-extension of the result of `__builtin_tzcnt()`
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lh_mouse at 126 dot com
  Target Milestone: ---

Given (godbolt: https://gcc.godbolt.org/z/hqKKW33T7):


```
long long
foo(long long x, unsigned bits)
  {
    return x + (unsigned) __builtin_ctz(bits);
  }
```

GCC output:
```
foo:
        bsf     eax, esi
        cdqe
        add     rax, rdi
        ret
```

Clang output:
```
foo:                                    # @foo
        bsf     eax, esi
        add     rax, rdi
        ret
```


The CDQE instruction is totally unnecessary, because BSF stores either a
non-negative value or an undefined one into its destination operand.

It's sad that `__builtin_ctz()` yields a signed type; but even we cast it, we
don't get anything better.

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

* [Bug middle-end/106231] sign-extension of the result of `__builtin_ctz`
  2022-07-08  8:55 [Bug target/106231] New: sign-extension of the result of `__builtin_tzcnt()` lh_mouse at 126 dot com
@ 2022-07-08 19:56 ` pinskia at gcc dot gnu.org
  2022-07-08 19:59 ` [Bug target/106231] " pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-08 19:56 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
          Component|target                      |middle-end
     Ever confirmed|0                           |1
           Severity|normal                      |enhancement
   Last reconfirmed|                            |2022-07-08
            Summary|sign-extension of the       |sign-extension of the
                   |result of                   |result of `__builtin_ctz`
                   |`__builtin_tzcnt()`         |

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.

There could be an easy pattern added for:
Failed to match this instruction:
(set (reg:DI 88 [ _1 ])
    (sign_extend:DI (ctz:SI (reg:SI 90))))

And for :

Failed to match this instruction:
(set (reg:DI 89 [ _1 ])
    (zero_extend:DI (ctz:SI (reg:SI 91))))

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

* [Bug target/106231] sign-extension of the result of `__builtin_ctz`
  2022-07-08  8:55 [Bug target/106231] New: sign-extension of the result of `__builtin_tzcnt()` lh_mouse at 126 dot com
  2022-07-08 19:56 ` [Bug middle-end/106231] sign-extension of the result of `__builtin_ctz` pinskia at gcc dot gnu.org
@ 2022-07-08 19:59 ` pinskia at gcc dot gnu.org
  2022-07-18  6:46 ` cvs-commit at gcc dot gnu.org
  2022-07-19  7:52 ` roger at nextmovesoftware dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-08 19:59 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Depends on|                            |71775

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Might be a dup of bug 71775. The problem with __builtin_ctz it is undefined at
0 for x86_64 (without one of the extensions enable).


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71775
[Bug 71775] Redundant move instruction for sign extension

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

* [Bug target/106231] sign-extension of the result of `__builtin_ctz`
  2022-07-08  8:55 [Bug target/106231] New: sign-extension of the result of `__builtin_tzcnt()` lh_mouse at 126 dot com
  2022-07-08 19:56 ` [Bug middle-end/106231] sign-extension of the result of `__builtin_ctz` pinskia at gcc dot gnu.org
  2022-07-08 19:59 ` [Bug target/106231] " pinskia at gcc dot gnu.org
@ 2022-07-18  6:46 ` cvs-commit at gcc dot gnu.org
  2022-07-19  7:52 ` roger at nextmovesoftware dot com
  3 siblings, 0 replies; 5+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-07-18  6:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Roger Sayle <sayle@gcc.gnu.org>:

https://gcc.gnu.org/g:2907bfc3412dd8aef6c6acc17f2152a4e0ac4979

commit r13-1726-g2907bfc3412dd8aef6c6acc17f2152a4e0ac4979
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Mon Jul 18 07:44:38 2022 +0100

    PR target/106231: Optimize (any_extend:DI (ctz:SI ...)) on x86_64.

    This patch resolves PR target/106231 by providing insns that recognize
    (zero_extend:DI (ctz:SI ...)) and (sign_extend:DI (ctz:SI ...)).  The
    result of ctz:SI is always between 0 and 32 (or undefined), so
    sign_extension is the same as zero_extension, and the result is already
    extended in the destination register.

    Things are a little complicated, because the existing implementation
    of *ctzsi2 handles multiple cases, including false dependencies, which
    we continue to support in this patch.

    2022-07-18  Roger Sayle  <roger@nextmovesoftware.com>

    gcc/ChangeLog
            PR target/106231
            * config/i386/i386.md (*ctzsidi2_<s>ext): New insn_and_split
            to recognize any_extend:DI of ctz:SI which is implicitly extended.
            (*ctzsidi2_<s>ext_falsedep): New define_insn to model a DImode
            extended ctz:SI that has preceding xor to break false dependency.

    gcc/testsuite/ChangeLog
            PR target/106231
            * gcc.target/i386/pr106231-1.c: New test case.
            * gcc.target/i386/pr106231-2.c: New test case.

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

* [Bug target/106231] sign-extension of the result of `__builtin_ctz`
  2022-07-08  8:55 [Bug target/106231] New: sign-extension of the result of `__builtin_tzcnt()` lh_mouse at 126 dot com
                   ` (2 preceding siblings ...)
  2022-07-18  6:46 ` cvs-commit at gcc dot gnu.org
@ 2022-07-19  7:52 ` roger at nextmovesoftware dot com
  3 siblings, 0 replies; 5+ messages in thread
From: roger at nextmovesoftware dot com @ 2022-07-19  7:52 UTC (permalink / raw)
  To: gcc-bugs

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

Roger Sayle <roger at nextmovesoftware dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |13.0
         Resolution|---                         |FIXED
             Status|NEW                         |RESOLVED
                 CC|                            |roger at nextmovesoftware dot com

--- Comment #4 from Roger Sayle <roger at nextmovesoftware dot com> ---
This should now be fixed on mainline (for GCC 13).  Unfortunately, this issue
is subtly different to PR rtl-optimization/71775 (which is trickier to solve).

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

end of thread, other threads:[~2022-07-19  7:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-08  8:55 [Bug target/106231] New: sign-extension of the result of `__builtin_tzcnt()` lh_mouse at 126 dot com
2022-07-08 19:56 ` [Bug middle-end/106231] sign-extension of the result of `__builtin_ctz` pinskia at gcc dot gnu.org
2022-07-08 19:59 ` [Bug target/106231] " pinskia at gcc dot gnu.org
2022-07-18  6:46 ` cvs-commit at gcc dot gnu.org
2022-07-19  7:52 ` roger at nextmovesoftware dot com

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).