From: Kyrylo Tkachov <[email protected]>
expand_CLMUL expands the call in the argument mode and then moves the
result into the lhs with emit_move_insn. When the lhs is a promoted
SUBREG, as for QImode and HImode values on AArch64, this leaves the bits
above the declared mode unextended. If the backend expands the clmul
through a wider instruction, such as PMULL on AArch64, those bits hold the
high half of the product and later users read a wrong value:
unsigned short f8 (unsigned char x, unsigned char y)
{
return (unsigned char) __builtin_clmulg (x, y);
}
f8 (0x7e, 0x9c) returned 0x3ae8 instead of 0xe8. This makes
libstdc++ 26_numerics/clmul/1.cc fail at runtime for unsigned short.
Use convert_move into SUBREG_REG for promoted SUBREGs, as expand_mul_overflow
does for its result.
Bootstrapped and tested on aarch64-none-linux-gnu.
Ok for trunk?
Thanks,
Kyrill
Cc: Jakub Jelinek <[email protected]>
gcc/ChangeLog:
* internal-fn.cc (expand_CLMUL): Use convert_move for promoted
SUBREG targets.
gcc/testsuite/ChangeLog:
* gcc.dg/builtin-clmulg-promoted-1.c: New test.
Signed-off-by: Kyrylo Tkachov <[email protected]>
---
gcc/internal-fn.cc | 5 +++-
.../gcc.dg/builtin-clmulg-promoted-1.c | 26 +++++++++++++++++++
2 files changed, 30 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gcc.dg/builtin-clmulg-promoted-1.c
diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc
index bf635c45a40..1d3163d1cac 100644
--- a/gcc/internal-fn.cc
+++ b/gcc/internal-fn.cc
@@ -6342,7 +6342,10 @@ expand_CLMUL (internal_fn, gcall *stmt)
res = expand_and (mode, res, mask, NULL_RTX);
}
rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
- emit_move_insn (target, res);
+ if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
+ convert_move (SUBREG_REG (target), res, SUBREG_PROMOTED_SIGN (target));
+ else
+ emit_move_insn (target, res);
}
void
diff --git a/gcc/testsuite/gcc.dg/builtin-clmulg-promoted-1.c
b/gcc/testsuite/gcc.dg/builtin-clmulg-promoted-1.c
new file mode 100644
index 00000000000..ba7be947c67
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/builtin-clmulg-promoted-1.c
@@ -0,0 +1,26 @@
+/* Check that narrow __builtin_clmulg results stored in promoted registers
+ are properly extended. */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+__attribute__ ((noipa)) unsigned short
+f8 (unsigned char x, unsigned char y)
+{
+ return (unsigned char) __builtin_clmulg (x, y);
+}
+
+__attribute__ ((noipa)) unsigned int
+f16 (unsigned short x, unsigned short y)
+{
+ return (unsigned short) __builtin_clmulg (x, y);
+}
+
+int
+main ()
+{
+ if (f8 (0x7e, 0x9c) != 0xe8)
+ __builtin_abort ();
+ if (f16 (0xc8aa, 0x7616) != 0xa55c)
+ __builtin_abort ();
+ return 0;
+}
--
2.54.0 (Apple Git-157)