lower-bitint: Handle casts from large/huge _BitInt to pointer/reference types [PR113692]

Message ID Zby7bjDEb0gsOAyi@tucnak
State Unresolved
Headers
Series lower-bitint: Handle casts from large/huge _BitInt to pointer/reference types [PR113692] |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Jakub Jelinek Feb. 2, 2024, 9:52 a.m. UTC
  Hi!

I thought one needs to cast first to pointer-sized integer before casting to
pointer, but apparently that is not the case.
So the following patch arranges for the large/huge _BitInt to
pointer/reference conversions to use the same code as for conversions
of them to small integral types.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2024-02-02  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/113692
	* gimple-lower-bitint.cc (bitint_large_huge::lower_stmt): Handle casts
	from large/huge BITINT_TYPEs to POINTER_TYPE/REFERENCE_TYPE as
	final_cast_p.

	* gcc.dg/bitint-82.c: New test.


	Jakub
  

Comments

Richard Biener Feb. 2, 2024, 10:19 a.m. UTC | #1
> Am 02.02.2024 um 10:53 schrieb Jakub Jelinek <jakub@redhat.com>:
> 
> Hi!
> 
> I thought one needs to cast first to pointer-sized integer before casting to
> pointer, but apparently that is not the case.
> So the following patch arranges for the large/huge _BitInt to
> pointer/reference conversions to use the same code as for conversions
> of them to small integral types.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

Ok

Richard 

> 2024-02-02  Jakub Jelinek  <jakub@redhat.com>
> 
>    PR tree-optimization/113692
>    * gimple-lower-bitint.cc (bitint_large_huge::lower_stmt): Handle casts
>    from large/huge BITINT_TYPEs to POINTER_TYPE/REFERENCE_TYPE as
>    final_cast_p.
> 
>    * gcc.dg/bitint-82.c: New test.
> 
> --- gcc/gimple-lower-bitint.cc.jj    2024-02-01 10:36:08.408867602 +0100
> +++ gcc/gimple-lower-bitint.cc    2024-02-01 11:47:21.233840956 +0100
> @@ -5264,7 +5264,8 @@ bitint_large_huge::lower_stmt (gimple *s
>    mergeable_cast_p = true;
>       else if (TREE_CODE (TREE_TYPE (rhs1)) == BITINT_TYPE
>           && bitint_precision_kind (TREE_TYPE (rhs1)) >= bitint_prec_large
> -           && INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
> +           && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
> +           || POINTER_TYPE_P (TREE_TYPE (lhs))))
>    {
>      final_cast_p = true;
>      if (TREE_CODE (rhs1) == SSA_NAME
> @@ -5393,8 +5394,9 @@ bitint_large_huge::lower_stmt (gimple *s
>     be needed.  */
>       gcc_assert (TYPE_PRECISION (lhs_type) <= 2 * limb_prec);
>       gimple *g;
> -      if (TREE_CODE (lhs_type) == BITINT_TYPE
> -      && bitint_precision_kind (lhs_type) == bitint_prec_middle)
> +      if ((TREE_CODE (lhs_type) == BITINT_TYPE
> +       && bitint_precision_kind (lhs_type) == bitint_prec_middle)
> +      || POINTER_TYPE_P (lhs_type))
>    lhs_type = build_nonstandard_integer_type (TYPE_PRECISION (lhs_type),
>                           TYPE_UNSIGNED (lhs_type));
>       m_data_cnt = 0;
> --- gcc/testsuite/gcc.dg/bitint-82.c.jj    2024-02-01 11:55:37.995866642 +0100
> +++ gcc/testsuite/gcc.dg/bitint-82.c    2024-02-01 11:54:16.998003815 +0100
> @@ -0,0 +1,18 @@
> +/* PR tree-optimization/113692 */
> +/* { dg-do compile { target bitint } } */
> +/* { dg-options "-O2 -std=c23" } */
> +
> +#if __BITINT_MAXWIDTH__ >= 135
> +_BitInt(135) i;
> +#else
> +_BitInt(63) i;
> +#endif
> +
> +void *
> +foo (void)
> +{
> +  void *ret = 0;
> +  if (i & 1)
> +    ret = (void *) 1;
> +  return ret;
> +}
> 
>    Jakub
>
  

Patch

--- gcc/gimple-lower-bitint.cc.jj	2024-02-01 10:36:08.408867602 +0100
+++ gcc/gimple-lower-bitint.cc	2024-02-01 11:47:21.233840956 +0100
@@ -5264,7 +5264,8 @@  bitint_large_huge::lower_stmt (gimple *s
 	mergeable_cast_p = true;
       else if (TREE_CODE (TREE_TYPE (rhs1)) == BITINT_TYPE
 	       && bitint_precision_kind (TREE_TYPE (rhs1)) >= bitint_prec_large
-	       && INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
+	       && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
+		   || POINTER_TYPE_P (TREE_TYPE (lhs))))
 	{
 	  final_cast_p = true;
 	  if (TREE_CODE (rhs1) == SSA_NAME
@@ -5393,8 +5394,9 @@  bitint_large_huge::lower_stmt (gimple *s
 	 be needed.  */
       gcc_assert (TYPE_PRECISION (lhs_type) <= 2 * limb_prec);
       gimple *g;
-      if (TREE_CODE (lhs_type) == BITINT_TYPE
-	  && bitint_precision_kind (lhs_type) == bitint_prec_middle)
+      if ((TREE_CODE (lhs_type) == BITINT_TYPE
+	   && bitint_precision_kind (lhs_type) == bitint_prec_middle)
+	  || POINTER_TYPE_P (lhs_type))
 	lhs_type = build_nonstandard_integer_type (TYPE_PRECISION (lhs_type),
 						   TYPE_UNSIGNED (lhs_type));
       m_data_cnt = 0;
--- gcc/testsuite/gcc.dg/bitint-82.c.jj	2024-02-01 11:55:37.995866642 +0100
+++ gcc/testsuite/gcc.dg/bitint-82.c	2024-02-01 11:54:16.998003815 +0100
@@ -0,0 +1,18 @@ 
+/* PR tree-optimization/113692 */
+/* { dg-do compile { target bitint } } */
+/* { dg-options "-O2 -std=c23" } */
+
+#if __BITINT_MAXWIDTH__ >= 135
+_BitInt(135) i;
+#else
+_BitInt(63) i;
+#endif
+
+void *
+foo (void)
+{
+  void *ret = 0;
+  if (i & 1)
+    ret = (void *) 1;
+  return ret;
+}