public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Iain Buclaw <ibuclaw@gdcproject.org>
To: gcc-patches@gcc.gnu.org
Cc: Iain Buclaw <ibuclaw@gdcproject.org>
Subject: [committed] d: Expand bsr intrinsic as `clz(arg) ^ (argsize - 1)'
Date: Sun, 11 Dec 2022 16:45:19 +0100	[thread overview]
Message-ID: <20221211154519.2681701-1-ibuclaw@gdcproject.org> (raw)

Hi,

This patch tweaks the code expansion of the D intrinsic bsr() function.

As well as removing unnecessary casts, this results in less temporaries
being generated during the initial gimple lowering pass.  Otherwise the
code generated is identical to the former intrinsic expansion.

Bootstrapped and regression tested on x86_64-linux-gnu/-m32.

Committed to mainline.

Regards,
Iain.

---
gcc/d/ChangeLog:

	* intrinsics.cc (expand_intrinsic_bsf): Fix comment.
	(expand_intrinsic_bsr): Use BIT_XOR_EXPR instead of MINUS_EXPR.
---
 gcc/d/intrinsics.cc | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/gcc/d/intrinsics.cc b/gcc/d/intrinsics.cc
index 6d9f74a6d7a..46380e512c4 100644
--- a/gcc/d/intrinsics.cc
+++ b/gcc/d/intrinsics.cc
@@ -525,7 +525,7 @@ call_builtin_fn (tree callexp, built_in_function code, int n, ...)
 static tree
 expand_intrinsic_bsf (tree callexp)
 {
-  /* The bsr() intrinsic gets turned into __builtin_ctz(arg).
+  /* The bsf() intrinsic gets turned into __builtin_ctz(arg).
      The return value is supposed to be undefined if arg is zero.  */
   tree arg = CALL_EXPR_ARG (callexp, 0);
   int argsize = TYPE_PRECISION (TREE_TYPE (arg));
@@ -554,11 +554,11 @@ expand_intrinsic_bsf (tree callexp)
 static tree
 expand_intrinsic_bsr (tree callexp)
 {
-  /* The bsr() intrinsic gets turned into (size - 1) - __builtin_clz(arg).
+  /* The bsr() intrinsic gets turned into __builtin_clz(arg) ^ (size - 1).
      The return value is supposed to be undefined if arg is zero.  */
   tree arg = CALL_EXPR_ARG (callexp, 0);
-  tree type = TREE_TYPE (arg);
-  int argsize = TYPE_PRECISION (type);
+  tree type = TREE_TYPE (callexp);
+  int argsize = TYPE_PRECISION (TREE_TYPE (arg));
 
   /* Which variant of __builtin_clz* should we call?  */
   built_in_function code = (argsize <= INT_TYPE_SIZE) ? BUILT_IN_CLZ
@@ -570,13 +570,8 @@ expand_intrinsic_bsr (tree callexp)
 
   tree result = call_builtin_fn (callexp, code, 1, arg);
 
-  /* Handle int -> long conversions.  */
-  if (TREE_TYPE (result) != type)
-    result = fold_convert (type, result);
-
-  result = fold_build2 (MINUS_EXPR, type,
-			build_integer_cst (argsize - 1, type), result);
-  return fold_convert (TREE_TYPE (callexp), result);
+  return fold_build2 (BIT_XOR_EXPR, type, result,
+		      build_integer_cst (argsize - 1, type));
 }
 
 /* Expand a front-end intrinsic call to INTRINSIC, which is either a call to
-- 
2.37.2


                 reply	other threads:[~2022-12-11 15:45 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20221211154519.2681701-1-ibuclaw@gdcproject.org \
    --to=ibuclaw@gdcproject.org \
    --cc=gcc-patches@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).