public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jakub at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/78103] Failure to optimize with __builtin_clzl
Date: Sat, 24 Jul 2021 12:28:42 +0000	[thread overview]
Message-ID: <bug-78103-4-snH3qRm372@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-78103-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I actually think this should be doable with combiner splitters.
For the
unsigned int findLastSet3(unsigned long x) {
  return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
}
or
unsigned int findLastSet4(unsigned int x) {
  return x ? 8 * sizeof(unsigned int) - __builtin_clz(x) : 0;
}
the combiner sees everything together, in the first case
(parallel [
        (set (reg:SI 84 [ <retval> ])
            (minus:SI (const_int 64 [0x40])
                (xor:SI (minus:SI (const_int 63 [0x3f])
                        (subreg:SI (clz:DI (reg/v:DI 85 [ x ])) 0))
                    (const_int 63 [0x3f]))))
        (clobber (reg:CC 17 flags))
    ])
and in the latter case
(parallel [
        (set (reg/v:SI 85 [ x ])
            (minus:SI (const_int 32 [0x20])
                (xor:SI (minus:SI (const_int 31 [0x1f])
                        (clz:SI (reg/v:SI 85 [ x ])))
                    (const_int 31 [0x1f]))))
        (clobber (reg:CC 17 flags))
    ])
so we just need to split it - into bsr and add 1 (of course only if
!TARGET_LZCNT, otherwise it is defined even for 0 and we don't know that it
will be UB on zero).
For the #c0 findLastSet we don't know that, but again we should know that it is
UB at zero when !TARGET_LZCNT and so we could first split
(set (reg:DI 87 [ _1 ])
    (xor:DI (sign_extend:DI (minus:SI (const_int 63 [0x3f])
                (subreg:SI (clz:DI (reg/v:DI 85 [ x ])) 0)))
        (const_int 63 [0x3f])))
into bsr and xor (i.e. get rid of the sign extension, if it is UB at zero, then
we know clz will be in range 0 to 63 and sign extension or zero extension are
the same and performed already when using the bs{q,l} instruction.

  parent reply	other threads:[~2021-07-24 12:28 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-78103-4@http.gcc.gnu.org/bugzilla/>
2021-07-24  6:20 ` pinskia at gcc dot gnu.org
2021-07-24 12:28 ` jakub at gcc dot gnu.org [this message]
2021-07-26 10:57 ` jakub at gcc dot gnu.org
2021-07-26 11:14 ` jakub at gcc dot gnu.org
2021-07-26 11:22 ` jakub at gcc dot gnu.org
2021-07-26 12:11 ` jakub at gcc dot gnu.org
2021-07-26 21:01 ` segher at gcc dot gnu.org
2021-07-26 21:08 ` jakub at gcc dot gnu.org
2021-07-26 21:26 ` segher at gcc dot gnu.org
2021-07-27  8:52 ` jakub at gcc dot gnu.org
2021-07-27 12:35 ` segher at gcc dot gnu.org
2021-07-27 12:41 ` jakub at gcc dot gnu.org
2021-07-27 13:53 ` jakub at gcc dot gnu.org
2021-07-27 15:25 ` segher at gcc dot gnu.org
2021-07-27 15:37 ` segher at gcc dot gnu.org
2021-07-31  7:21 ` cvs-commit at gcc dot gnu.org
2021-07-31  8:24 ` jakub at gcc dot gnu.org
2021-08-01 20:33 ` cvs-commit 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-78103-4-snH3qRm372@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).