On Tue, 10 Nov 2026, Jakub Jelinek wrote: > Hi! > > The following patch implements another optimization for IFN_CLMUL and > IFN_CLMUL_WIDEN on top of the > https://gcc.gnu.org/pipermail/gcc-patches/2026-November/737961.html > patch. If bitexpand_optab is implemented in hw, __builtin_clmulg (x, x) > can be implemented as __builtin_bitexpandg (x, std::bit_repeat ((decltype > (x))1, 2)) > i.e. e.g. for unsigned long long as IFN_BITEXPAND (x, 0x5555555555555555ULL). > On x86 with BMI2 the pdep should be even better than PCLMULDQ pclmuldq, > because pdep isn't a vector insn. > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk > (once the C/C++ parts of the above mentioned patch are acked)? > > Alternatively, instead of adding op_eq argument we could arrange in > the callers in the operand_equal_p (arg0, arg1, 0) case to simply pass > op0, op0 arguments and thus ensure they are pointer equal. But I'm afraid > it would be slightly larger code.
OK. Thanks, Richard. > 2026-11-10 Jakub Jelinek <[email protected]> > > * internal-fn.cc (expand_clmul): Add OP_EQ argument. If true, > attempt to optimize clmul (x, x) using bitexpand (x, 0x55555555U) if > the bitexpand_optab supports it. > (expand_CLMUL, expand_CLMUL_WIDEN): Adjust expand_clmul callers. > > * c-c++-common/builtin-clmulg-4.c: New test. > * gcc.target/i386/bmi2-builtin-clmulg-4.c: New test. > > --- a/gcc/internal-fn.cc 2026-11-09 11:43:44.549976617 +0100 > +++ b/gcc/internal-fn.cc 2026-11-09 21:27:55.594430305 +0100 > @@ -6107,11 +6107,12 @@ expand_BITREVERSE (internal_fn fn, gcall > } > > /* Expand CLMUL (OP0, OP1) where operands are in OP_MODE and have > - minimum unsigned precision PREC0 and PREC1. Compute result in MODE. */ > + minimum unsigned precision PREC0 and PREC1. If OP_EQ, op0 and op1 > + have the same value. Compute result in MODE. */ > > static rtx > expand_clmul (machine_mode mode, machine_mode op_mode, rtx op0, rtx op1, > - unsigned int prec0, unsigned int prec1) > + unsigned int prec0, unsigned int prec1, bool op_eq) > { > unsigned int op_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> > (op_mode)); > unsigned int prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)); > @@ -6131,6 +6132,23 @@ expand_clmul (machine_mode mode, machine > = GET_MODE_PRECISION (as_a <scalar_int_mode> (cmode)); > if (prec0 > this_prec || prec1 > this_prec) > continue; > + if (op_eq > + && (prec0 + prec1 - 1 <= this_prec || prec <= this_prec) > + && optab_handler (bitexpand_optab, cmode) != CODE_FOR_nothing) > + { > + /* Optimize clmul (x, x) using bitexpand (x, 0x55555555U) > + if supported in hw. */ > + rtx_insn *start = get_last_insn (); > + rtx op0c = convert_modes (cmode, op_mode, op0, 1); > + rtx op1c > + = immed_wide_int_const (wi::bit_repeat (wide_int::from (1, 2, > + UNSIGNED), > + this_prec), cmode); > + if (rtx res = expand_binop (cmode, bitexpand_optab, op0c, op1c, > + NULL_RTX, 1, OPTAB_DIRECT)) > + return convert_modes (mode, cmode, res, 1); > + delete_insns_since (start); > + } > if (optab_handler (clmul_optab, cmode) != CODE_FOR_nothing > && (prec0 + prec1 - 1 <= this_prec || prec <= this_prec)) > { > @@ -6330,7 +6348,8 @@ expand_CLMUL (internal_fn, gcall *stmt) > unsigned int prec1 = get_min_precision (arg1, UNSIGNED); > rtx op0 = expand_normal (arg0); > rtx op1 = expand_normal (arg1); > - rtx res = expand_clmul (mode, mode, op0, op1, prec0, prec1); > + rtx res = expand_clmul (mode, mode, op0, op1, prec0, prec1, > + operand_equal_p (arg0, arg1, 0)); > if (TYPE_PRECISION (TREE_TYPE (arg0)) < GET_MODE_PRECISION (mode)) > { > rtx mask > @@ -6358,6 +6377,7 @@ expand_CLMUL_WIDEN (internal_fn, gcall * > rtx op1 = expand_normal (arg1); > rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); > rtx bitint_mask = NULL_RTX; > + bool op_eq = operand_equal_p (arg0, arg1, 0); > if (type_prec != prec) > bitint_mask > = immed_wide_int_const (wi::mask (type_prec, false, prec), mode); > @@ -6365,7 +6385,7 @@ expand_CLMUL_WIDEN (internal_fn, gcall * > { > /* The whole product fits in MODE. For a type narrower than MODE > the bits above its precision are the high half. */ > - rtx reslo = expand_clmul (mode, mode, op0, op1, prec0, prec1); > + rtx reslo = expand_clmul (mode, mode, op0, op1, prec0, prec1, op_eq); > rtx reshi = const0_rtx; > if (prec0 + prec1 - 1 > type_prec) > reshi = expand_shift (RSHIFT_EXPR, mode, reslo, type_prec, NULL_RTX, 1); > @@ -6399,7 +6419,7 @@ expand_CLMUL_WIDEN (internal_fn, gcall * > if (GET_MODE_2XWIDER_MODE (mode).exists (&wmode) > && targetm.scalar_mode_supported_p (wmode)) > { > - rtx res = expand_clmul (wmode, mode, op0, op1, prec0, prec1); > + rtx res = expand_clmul (wmode, mode, op0, op1, prec0, prec1, op_eq); > rtx reslo = convert_modes (mode, wmode, res, 1); > if (bitint_mask) > reslo = expand_and (mode, reslo, bitint_mask, NULL_RTX); > @@ -6419,13 +6439,13 @@ expand_CLMUL_WIDEN (internal_fn, gcall * > || (prec == 2 * UNITS_PER_WORD > && optab_handler (bitreverse_optab, word_mode) != CODE_FOR_nothing)) > { > - rtx reslo = expand_clmul (mode, mode, op0, op1, prec0, prec1); > + rtx reslo = expand_clmul (mode, mode, op0, op1, prec0, prec1, op_eq); > if (bitint_mask) > reslo = expand_and (mode, reslo, bitint_mask, NULL_RTX); > write_complex_part (target, reslo, false, true); > rtx revx = expand_unop (mode, bitreverse_optab, op0, NULL_RTX, 1); > rtx revy = expand_unop (mode, bitreverse_optab, op1, NULL_RTX, 1); > - rtx reshi = expand_clmul (mode, mode, revx, revy, prec, prec); > + rtx reshi = expand_clmul (mode, mode, revx, revy, prec, prec, op_eq); > reshi = expand_unop (mode, bitreverse_optab, reshi, NULL_RTX, 1); > if (bitint_mask) > reshi = expand_and (mode, reshi, bitint_mask, NULL_RTX); > @@ -6450,16 +6470,16 @@ expand_CLMUL_WIDEN (internal_fn, gcall * > rtx x1 = expand_shift (RSHIFT_EXPR, mode, op0, hprec, NULL_RTX, 1); > rtx y0 = expand_binop (mode, and_optab, op1, mask, NULL_RTX, 1, olw); > rtx y1 = expand_shift (RSHIFT_EXPR, mode, op1, hprec, NULL_RTX, 1); > - rtx l = expand_clmul (mode, mode, x0, y0, hprec, hprec); > + rtx l = expand_clmul (mode, mode, x0, y0, hprec, hprec, op_eq); > rtx t0 = expand_binop (mode, xor_optab, x0, x1, NULL_RTX, 1, olw); > rtx t1 = expand_binop (mode, xor_optab, y0, y1, NULL_RTX, 1, olw); > - rtx m = expand_clmul (mode, mode, t0, t1, hprec, hprec); > + rtx m = expand_clmul (mode, mode, t0, t1, hprec, hprec, op_eq); > rtx h; > if (prec0 <= hprec || prec1 <= hprec) > h = const0_rtx; > else > h = expand_clmul (mode, mode, x1, y1, MIN (hprec, prec0 - hprec), > - MIN (hprec, prec1 - hprec)); > + MIN (hprec, prec1 - hprec), op_eq); > t0 = expand_binop (mode, xor_optab, l, h, NULL_RTX, 1, olw); > m = expand_binop (mode, xor_optab, m, t0, NULL_RTX, 1, olw); > t0 = expand_shift (LSHIFT_EXPR, mode, m, hprec, NULL_RTX, 1); > --- a/gcc/testsuite/c-c++-common/builtin-clmulg-4.c 2026-11-09 > 12:03:56.663628452 +0100 > +++ b/gcc/testsuite/c-c++-common/builtin-clmulg-4.c 2026-11-09 > 12:16:03.998824213 +0100 > @@ -0,0 +1,108 @@ > +/* Test __builtin_clmulg. */ > +/* { dg-do run } */ > +/* { dg-options "-O2" } */ > + > +[[gnu::noipa]] unsigned char > +clmul8 (unsigned char x) > +{ > + return __builtin_clmulg (x, x); > +} > + > +[[gnu::noipa]] unsigned char > +clmul8w (unsigned char x, unsigned char *y) > +{ > + return __builtin_clmulg (x, x, y); > +} > + > +[[gnu::noipa]] unsigned short > +clmul16 (unsigned short x) > +{ > + return __builtin_clmulg (x, x); > +} > + > +[[gnu::noipa]] unsigned short > +clmul16w (unsigned short x, unsigned short *y) > +{ > + return __builtin_clmulg (x, x, y); > +} > + > +[[gnu::noipa]] unsigned int > +clmul32 (unsigned int x) > +{ > + return __builtin_clmulg (x, x); > +} > + > +[[gnu::noipa]] unsigned int > +clmul32w (unsigned int x, unsigned int *y) > +{ > + return __builtin_clmulg (x, x, y); > +} > + > +[[gnu::noipa]] unsigned long long > +clmul64 (unsigned long long x) > +{ > + return __builtin_clmulg (x, x); > +} > + > +[[gnu::noipa]] unsigned long long > +clmul64w (unsigned long long x, unsigned long long *y) > +{ > + return __builtin_clmulg (x, x, y); > +} > + > +#ifdef __SIZEOF_INT128__ > +[[gnu::noipa]] unsigned __int128 > +clmul128 (unsigned __int128 x) > +{ > + return __builtin_clmulg (x, x); > +} > + > +[[gnu::noipa]] unsigned __int128 > +clmul128w (unsigned __int128 x, unsigned __int128 *y) > +{ > + return __builtin_clmulg (x, x, y); > +} > +#endif > + > +int > +main () > +{ > + unsigned char h8, e8; > + unsigned short h16, e16; > + unsigned int h32, e32; > + unsigned long long h64, e64; > + if (clmul8 (0x64) > + != __builtin_clmulg ((unsigned char) 0x64, (unsigned char) 0x64) > + || (clmul8w (0x64, &h8) > + != __builtin_clmulg ((unsigned char) 0x64, (unsigned char) 0x64, > + &e8)) > + || h8 != e8 > + || (clmul16 (0x477b) > + != __builtin_clmulg ((unsigned short) 0x477b, > + (unsigned short) 0x477b)) > + || (clmul16w (0x477b, &h16) > + != __builtin_clmulg ((unsigned short) 0x477b, > + (unsigned short) 0x477b, &e16)) > + || h16 != e16 > + || (clmul32 (0xac668e06U) > + != __builtin_clmulg (0xac668e06U, 0xac668e06U)) > + || (clmul32w (0xac668e06U, &h32) > + != __builtin_clmulg (0xac668e06U, 0xac668e06U, &e32)) > + || h32 != e32 > + || (clmul64 (0xd5786b341027cb20ULL) > + != __builtin_clmulg (0xd5786b341027cb20ULL, 0xd5786b341027cb20ULL)) > + || (clmul64w (0xd5786b341027cb20ULL, &h64) > + != __builtin_clmulg (0xd5786b341027cb20ULL, 0xd5786b341027cb20ULL, > + &e64)) > + || h64 != e64) > + __builtin_abort (); > +#ifdef __SIZEOF_INT128__ > + unsigned __int128 h128, e128; > + unsigned __int128 v128 = ((((unsigned __int128) 0x6e3e755379242a1fULL) << > 64) > + | 0x49cb5521019109bbULL); > + if (clmul128 (v128) != __builtin_clmulg (v128, v128) > + || clmul128w (v128, &h128) != __builtin_clmulg (v128, v128, &e128) > + || h128 != e128) > + __builtin_abort (); > +#endif > +} > --- a/gcc/testsuite/gcc.target/i386/bmi2-builtin-clmulg-4.c 2026-11-09 > 12:27:56.364236761 +0100 > +++ b/gcc/testsuite/gcc.target/i386/bmi2-builtin-clmulg-4.c 2026-11-09 > 12:27:25.719649154 +0100 > @@ -0,0 +1,15 @@ > +/* { dg-do run } */ > +/* { dg-require-effective-target bmi2 } */ > +/* { dg-options "-O2 -mbmi2 -mno-pclmul" } */ > + > +#include "bmi2-check.h" > + > +#define main do_main > + > +#include "../../c-c++-common/builtin-clmulg-4.c" > + > +static void > +bmi2_test (void) > +{ > + do_main (); > +} > > Jakub > > -- Richard Biener <[email protected]> SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri; (HRB 36809, AG Nuernberg)
