When passing 0xff as an unsigned char function argument, the C frontend
promotion will promote it to int:

<integer_cst 0x7fffe6aa23a8 type <integer_type 0x7fffe98225e8 int> constant 255>

and expand_normal always returns the rtx value using the sign-extended
representation,

(const_int 255 [0xff])

If the C frontend doesn't promote unsigned char to int, expand_normal will
get

<integer_cst 0x7fffe9824018 type <integer_type 0x7fffe9822348 unsigned char > co
nstant 255>

and return

gnu-tgl-3:pts/6[168]> head -50
/tmp/0001-i386-Add-ix86_expand_integer_cst_argument.patch
>From d9dddaad6254cca11b0a55c18291180d6d8d7524 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.to...@gmail.com>
Date: Tue, 12 Nov 2024 09:03:31 +0800
Subject: [PATCH] i386: Add ix86_expand_integer_cst_argument

When passing 0xff as an unsigned char function argument, the C frontend
promotion will promote it to int:

<integer_cst 0x7fffe6aa23a8 type <integer_type 0x7fffe98225e8 int> constant 255>

and expand_normal always returns the rtx value using the sign-extended
representation,

(const_int 255 [0xff])

If the C frontend doesn't promote unsigned char to int, expand_normal will
get

<integer_cst 0x7fffe9824018 type <integer_type 0x7fffe9822348 unsigned char > co
nstant 255>

and return

(const_int -1 [0xffffffffffffffff])

Add ix86_expand_integer_cst_argument to expand the argument before calling
fixup_modeless_constant to extract the 8-bit/16-bit integer constants to
always return

(const_int 255 [0xff])

in this case.

PR middle-end/14907
* config/i386/i386-expand.cc (ix86_expand_integer_cst_argument):
New function.
(ix86_expand_args_builtin): Call ix86_expand_integer_cst_argument
to expand the argument before calling fixup_modeless_constant.
(ix86_expand_round_builtin): Likewise.
(ix86_expand_special_args_builtin): Likewise.
(ix86_expand_builtin): Likewise.

-- 
H.J.
From d9dddaad6254cca11b0a55c18291180d6d8d7524 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.to...@gmail.com>
Date: Tue, 12 Nov 2024 09:03:31 +0800
Subject: [PATCH] i386: Add ix86_expand_integer_cst_argument

When passing 0xff as an unsigned char function argument, the C frontend
promotion will promote it to int:

<integer_cst 0x7fffe6aa23a8 type <integer_type 0x7fffe98225e8 int> constant 255>

and expand_normal always returns the rtx value using the sign-extended
representation,

(const_int 255 [0xff])

If the C frontend doesn't promote unsigned char to int, expand_normal will
get

<integer_cst 0x7fffe9824018 type <integer_type 0x7fffe9822348 unsigned char > co
nstant 255>

and return

(const_int -1 [0xffffffffffffffff])

Add ix86_expand_integer_cst_argument to expand the argument before calling
fixup_modeless_constant to extract the 8-bit/16-bit integer constants to
always return

(const_int 255 [0xff])

in this case.

	PR middle-end/14907
	* config/i386/i386-expand.cc (ix86_expand_integer_cst_argument):
	New function.
	(ix86_expand_args_builtin): Call ix86_expand_integer_cst_argument
	to expand the argument before calling fixup_modeless_constant.
	(ix86_expand_round_builtin): Likewise.
	(ix86_expand_special_args_builtin): Likewise.
	(ix86_expand_builtin): Likewise.

Signed-off-by: H.J. Lu <hjl.to...@gmail.com>
---
 gcc/config/i386/i386-expand.cc | 45 ++++++++++++++++++++++++++++++----
 1 file changed, 40 insertions(+), 5 deletions(-)

diff --git a/gcc/config/i386/i386-expand.cc b/gcc/config/i386/i386-expand.cc
index 5c4a8e07d62..6b075fa6ee7 100644
--- a/gcc/config/i386/i386-expand.cc
+++ b/gcc/config/i386/i386-expand.cc
@@ -11243,6 +11243,41 @@ fixup_modeless_constant (rtx x, machine_mode mode)
   return x;
 }
 
+/* Expand the outgoing argument ARG.  */
+
+static rtx
+ix86_expand_integer_cst_argument (tree arg)
+{
+  /* expand_normal gets
+
+     <integer_cst 0x7fffe9824018 type <integer_type 0x7fffe9822348 unsigned char > constant 255>
+
+     when passing 0xff as an unsigned char function argument.  Since
+     expand_normal always returns the rtx value using the sign-extended
+     representation, it returns
+
+     (const_int -1 [0xffffffffffffffff])
+
+     extract the 8-bit/16-bit integer constants to always return
+
+     (const_int 255 [0xff])
+
+     in this case.  */
+
+  if (TREE_CODE (arg) == INTEGER_CST)
+    {
+      tree type = TREE_TYPE (arg);
+      if (INTEGRAL_TYPE_P (type)
+	  && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
+	{
+	  HOST_WIDE_INT cst = tree_to_shwi (arg);
+	  return GEN_INT (cst);
+	}
+    }
+
+  return expand_normal (arg);
+}
+
 /* Subroutine of ix86_expand_builtin to take care of insns with
    variable number of operands.  */
 
@@ -12135,7 +12170,7 @@ ix86_expand_args_builtin (const struct builtin_description *d,
   for (i = 0; i < nargs; i++)
     {
       tree arg = CALL_EXPR_ARG (exp, i);
-      rtx op = expand_normal (arg);
+      rtx op = ix86_expand_integer_cst_argument (arg);
       machine_mode mode = insn_p->operand[i + 1].mode;
       /* Need to fixup modeless constant before testing predicate.  */
       op = fixup_modeless_constant (op, mode);
@@ -12870,7 +12905,7 @@ ix86_expand_round_builtin (const struct builtin_description *d,
   for (i = 0; i < nargs; i++)
     {
       tree arg = CALL_EXPR_ARG (exp, i);
-      rtx op = expand_normal (arg);
+      rtx op = ix86_expand_integer_cst_argument (arg);
       machine_mode mode = insn_p->operand[i + 1].mode;
       bool match = insn_p->operand[i + 1].predicate (op, mode);
 
@@ -13355,7 +13390,7 @@ ix86_expand_special_args_builtin (const struct builtin_description *d,
       machine_mode mode = insn_p->operand[i + 1].mode;
 
       arg = CALL_EXPR_ARG (exp, i + arg_adjust);
-      op = expand_normal (arg);
+      op = ix86_expand_integer_cst_argument (arg);
 
       if (i == memory)
 	{
@@ -15485,7 +15520,7 @@ rdseed_step:
       op0 = expand_normal (arg0);
       op1 = expand_normal (arg1);
       op2 = expand_normal (arg2);
-      op3 = expand_normal (arg3);
+      op3 = ix86_expand_integer_cst_argument (arg3);
       op4 = expand_normal (arg4);
       /* Note the arg order is different from the operand order.  */
       mode0 = insn_data[icode].operand[1].mode;
@@ -15700,7 +15735,7 @@ rdseed_step:
       arg3 = CALL_EXPR_ARG (exp, 3);
       arg4 = CALL_EXPR_ARG (exp, 4);
       op0 = expand_normal (arg0);
-      op1 = expand_normal (arg1);
+      op1 = ix86_expand_integer_cst_argument (arg1);
       op2 = expand_normal (arg2);
       op3 = expand_normal (arg3);
       op4 = expand_normal (arg4);
-- 
2.47.0

Reply via email to