[PATCH] c, c++, middle-end, i386, libstdc++, v2: Introduce __builtin_bit{compress,expand}g builtins [PR128459]

Jakub Jelinek jakub@redhat.com
Mon Nov 9 08:20:10 GMT 2026


On Thu, Nov 05, 2026 at 02:57:50PM +0100, Jakub Jelinek wrote:
> The following patch on top of the __builtin_clmulg patch
> https://gcc.gnu.org/pipermail/gcc-patches/2026-October/736969.html
> implements builtins to optimize the recently added std::bit_compress
> and std::bit_expand function templates.
> Similarly to the reasons for __builtin_clmulg, various targets have
> instructions to support this (x86 BMI2 pext/pdep, SVE2 has bext/bdep,
> RISC-V has something, ...) and even when not, e.g. some parts of
> the code can be optimized using __builtin_clmulg if implemented
> in hardware (otherwise it would be pessimization rather than optimization).
> 
> So far lightly tested on the new tests and libstdc++ bit.permute tests,
> ok for trunk if it passes full bootstrap/regtest?

The previous patch no longer applies due to c-parser.cc changes in the
__builtin_clmulg commit, here is an updated version.

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

2026-11-09  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/128459
	* doc/extend.texi (__builtin_bitcompressg, __builtin_bitexpandg):
	Document.
	* builtins.h (fold_build_builtin_bitcompressg_bitexpandg): Declare.
	* fold-const-call.cc (fold_const_call_sss): Handle IFN_BITCOMPRESS
	and IFN_BITEXPAND.
	* internal-fn.h (expand_BITCOMPRESS, expand_BITEXPAND): Declare.
	* fold-const.cc (tree_call_nonnegative_p): Handle IFN_BITCOMPRESS
	and IFN_BITEXPAND.
	* wide-int.cc (wi::bit_compress, wi::bit_expand): New functions.
	* wide-int.h (wi::bit_compress, wi::bit_expand): Declare.
	* builtins.cc (fold_build_builtin_bitcompressg_bitexpandg): New
	function.
	* internal-fn.def (BITCOMPRESS, BITEXPAND): New internal functions.
	* internal-fn.cc (expand_BITCOMPRESS, expand_BITEXPAND): New functions.
	* optabs.def (bitcompress_optab, bitexpand_optab): New optabs.
	* gimple-lower-bitint.cc (bitint_large_huge::lower_stmt): Handle
	store temporarily without vdef in SSA form.
	(lower_bitcompress_bitexpand): New function.
	(gimple_lower_bitint): Lower IFN_BITCOMPRESS and IFN_BITEXPAND calls
	before lowering.
	* match.pd (.BITCOMPRESS, .BITEXPAND): New simplifications.
	* config/i386/i386.md (bitexpand<mode>3, bitcompress<mode>3): New
	BMI2 define_expand patterns.
gcc/c-family/
	* c-common.h (enum rid): Add RID_BUILTIN_BITCOMPRESSG and
	RID_BUILTIN_BITEXPANDG enumerators.
	* c-common.cc (c_common_reswords): Add __builtin_bitcompressg and
	__builtin_bitexpandg entries.
gcc/c/
	* c-parser.cc (c_parser_postfix_expression): Handle
	RID_BUILTIN_BITCOMPRESSG and RID_BUILTIN_BITEXPANDG.
	* c-decl.cc (names_builtin_p): Likewise.
gcc/cp/
	* parser.cc (cp_parser_postfix_expression): Handle
	RID_BUILTIN_BITCOMPRESSG and RID_BUILTIN_BITEXPANDG.
	* typeck.cc (build_x_bitcompressg_bitexpandg): New function.
	* cp-tree.h (build_x_bitcompressg_bitexpandg): Declare.
	* pt.cc (tsubst_expr): Handle IFN_BITCOMPRESS and IFN_BITEXPAND.
	* mangle.cc (write_expression): Likewise.
	* cp-objcp-common.cc (names_builtin_p): Handle
	RID_BUILTIN_BITCOMPRESSG and RID_BUILTIN_BITEXPANDG.
	* cp-gimplify.cc (cp_gimplify_expr): Handle IFN_BITCOMPRESS and
	IFN_BITEXPAND.
	* constexpr.cc (cxx_eval_internal_function): Likewise.
	(potential_constant_expression_1): Likewise.
gcc/testsuite/
	* c-c++-common/builtin-bitcompressg-1.c: New test.
	* c-c++-common/builtin-bitcompressg-2.c: New test.
	* c-c++-common/builtin-bitexpandg-1.c: New test.
	* c-c++-common/builtin-bitexpandg-2.c: New test.
	* gcc.target/i386/bmi2-builtin-bitcompressg-1.c: New test.
	* gcc.target/i386/bmi2-builtin-bitcompressg-2.c: New test.
	* gcc.target/i386/bmi2-builtin-bitexpandg-1.c: New test.
	* gcc.target/i386/bmi2-builtin-bitexpandg-2.c: New test.
	* gcc.target/i386/pclmul-builtin-bitcompressg-1.c: New test.
	* gcc.target/i386/pclmul-builtin-bitexpandg-1.c: New test.
libstdc++-v3/
	* include/std/bit (std::bit_compress): Use __builtin_bitcompressg
	if available.
	(std::bit_expand): Use __builtin_bitexpandg if available.

--- a/gcc/doc/extend.texi	2026-11-03 11:23:44.297317597 +0100
+++ b/gcc/doc/extend.texi	2026-11-04 17:23:29.429822036 +0100
@@ -18119,6 +18119,46 @@ of the full result, i.e. @code{0x2220220
 
 @enddefbuiltin
 
+@defbuiltin{@var{type} __builtin_bitcompressg (@var{type} @var{x}, @var{type} @var{m})}
+
+This type-generic built-in function returns bit compression (sometimes
+called parallel bit extract) of @var{x} using mask @var{m}.
+@var{type} may be any unsigned integral type.
+For each set bit in @var{m} the corresponding bit in @var{x} is tightly
+packed in the least significant bits of the result.  The bit corresponding
+to least significant set bit of @var{m} is the least significant bit of the
+result.
+
+@smallexample
+  unsigned int x = 0xa96e, m = 0x5fb3;
+  unsigned int r = __builtin_bitcompressg (x, m);
+@end smallexample
+
+@var{r} will be set to @code{0x12a}.
+
+@enddefbuiltin
+
+@defbuiltin{@var{type} __builtin_bitexpandg (@var{type} @var{x}, @var{type} @var{m})}
+
+This type-generic built-in function returns bit expansion (sometimes
+called parallel bit deposit) of @var{x} using mask @var{m}.
+@var{type} may be any unsigned integral type.
+For each set bit in @var{m} a bit from @var{x} starting with least
+significant bit is shifted into the position of the bit in @var{m} and
+bitwise ored into the result.  This is inverse operation for
+@code{__builtin_compressg}, except that bits not set in @var{m} are cleared.
+So @code{__builtin_expandg (__builtin_compressg (@var{x}, @var{m}), @var{m})}
+is always equal to @code{@var{x} & @var{m}}.
+
+@smallexample
+  unsigned int x = 0x12a, m = 0x5fb3;
+  unsigned int r = __builtin_bitexpandg (x, m);
+@end smallexample
+
+@var{r} will be set to @code{0x922}.
+
+@enddefbuiltin
+
 @defbuiltin{@var{type} __builtin_speculation_safe_value (@var{type} @var{val}, @var{type} @var{failval})}
 
 This built-in function can be used to help mitigate against unsafe
--- a/gcc/config/i386/i386.md	2026-11-02 23:30:10.317274133 +0100
+++ b/gcc/config/i386/i386.md	2026-11-04 17:23:29.407822328 +0100
@@ -23408,6 +23408,13 @@ (define_insn "bmi2_pdep_<mode>3"
    (set_attr "prefix" "vex")
    (set_attr "mode" "<MODE>")])
 
+(define_expand "bitexpand<mode>3"
+  [(set (match_operand:SWI48 0 "register_operand")
+	(unspec:SWI48 [(match_operand:SWI48 1 "register_operand")
+		       (match_operand:SWI48 2 "nonimmediate_operand")]
+		       UNSPEC_PDEP))]
+  "TARGET_BMI2")
+
 (define_insn "bmi2_pext_<mode>3"
   [(set (match_operand:SWI48 0 "register_operand" "=r")
         (unspec:SWI48 [(match_operand:SWI48 1 "register_operand" "r")
@@ -23419,6 +23426,13 @@ (define_insn "bmi2_pext_<mode>3"
    (set_attr "prefix" "vex")
    (set_attr "mode" "<MODE>")])
 
+(define_expand "bitcompress<mode>3"
+  [(set (match_operand:SWI48 0 "register_operand")
+	(unspec:SWI48 [(match_operand:SWI48 1 "register_operand")
+		       (match_operand:SWI48 2 "nonimmediate_operand")]
+		       UNSPEC_PEXT))]
+  "TARGET_BMI2")
+
 ;; TBM instructions.
 (define_insn "@tbm_bextri_<mode>"
   [(set (match_operand:SWI48 0 "register_operand" "=r")
--- a/gcc/fold-const-call.cc	2026-11-03 11:23:44.300317557 +0100
+++ b/gcc/fold-const-call.cc	2026-11-04 17:23:29.399925447 +0100
@@ -1808,6 +1808,14 @@ fold_const_call_sss (wide_int *result, c
       *result = wi::clmul (arg0, arg1, precision);
       return true;
 
+    case CFN_BITCOMPRESS:
+      *result = wi::bit_compress (arg0, arg1, precision);
+      return true;
+
+    case CFN_BITEXPAND:
+      *result = wi::bit_expand (arg0, arg1, precision);
+      return true;
+
     default:
       return false;
     }
--- a/gcc/builtins.cc	2026-11-03 11:23:44.306025445 +0100
+++ b/gcc/builtins.cc	2026-11-04 17:23:29.410913909 +0100
@@ -10778,6 +10778,31 @@ fold_build_builtin_clmulg (location_t lo
   return build2_loc (loc, COMPOUND_EXPR, type, store, intres);
 }
 
+/* Fold __builtin_bitcompressg (arg1, arg2) or
+   __builtin_bitexpandg (arg1, arg2) into IFN_BITCOMPRESS or IFN_BITEXPAND
+   internal call.  */
+
+tree
+fold_build_builtin_bitcompressg_bitexpandg (location_t loc, internal_fn ifn,
+					    tree arg1, tree arg2)
+{
+  tree type = TYPE_MAIN_VARIANT (TREE_TYPE (arg1));
+  if (TREE_CODE (arg1) == INTEGER_CST
+      && !TREE_OVERFLOW (arg1)
+      && TREE_CODE (arg2) == INTEGER_CST
+      && !TREE_OVERFLOW (arg2))
+    {
+      unsigned prec = TYPE_PRECISION (type);
+      wide_int res;
+      if (ifn == IFN_BITCOMPRESS)
+	res = wi::bit_compress (wi::to_wide (arg1), wi::to_wide (arg2), prec);
+      else
+	res = wi::bit_expand (wi::to_wide (arg1), wi::to_wide (arg2), prec);
+      return wide_int_to_tree (type, res);
+    }
+  return build_call_expr_internal_loc (loc, ifn, type, 2, arg1, arg2);
+}
+
 /* Fold a call to __builtin_FILE to a constant string.  */
 
 static inline tree
--- a/gcc/wide-int.h	2026-11-03 11:23:44.305417129 +0100
+++ b/gcc/wide-int.h	2026-11-04 17:23:29.437076517 +0100
@@ -3985,6 +3985,10 @@ namespace wi
 
   wide_int bit_repeat (const wide_int_ref &, unsigned int);
   wide_int clmul (const wide_int_ref &, const wide_int_ref &, unsigned int);
+  wide_int bit_compress (const wide_int_ref &, const wide_int_ref &,
+			 unsigned int);
+  wide_int bit_expand (const wide_int_ref &, const wide_int_ref &,
+		       unsigned int);
 }
 
 /* Return a PRECISION-bit integer in which the low WIDTH bits are set
--- a/gcc/optabs.def	2026-11-03 11:23:44.308599512 +0100
+++ b/gcc/optabs.def	2026-11-04 17:23:29.398822447 +0100
@@ -579,3 +579,5 @@ OPTAB_D (iorn_optab, "iorn$a3")
 OPTAB_D (aop_optab, "aop$a3")
 OPTAB_D (clmul_optab, "clmul$a3")
 OPTAB_D (clmul_widen_optab, "clmul_widen$a3")
+OPTAB_D (bitcompress_optab, "bitcompress$a3")
+OPTAB_D (bitexpand_optab, "bitexpand$a3")
--- a/gcc/c/c-decl.cc	2026-11-03 11:23:44.272226079 +0100
+++ b/gcc/c/c-decl.cc	2026-11-04 17:23:29.434061572 +0100
@@ -11934,6 +11934,8 @@ names_builtin_p (const char *name)
   switch (C_RID_CODE (id))
     {
     case RID_BUILTIN_ASSOC_BARRIER:
+    case RID_BUILTIN_BITCOMPRESSG:
+    case RID_BUILTIN_BITEXPANDG:
     case RID_BUILTIN_BITREVERSEG:
     case RID_BUILTIN_BSWAPG:
     case RID_BUILTIN_CLMULG:
--- a/gcc/c/c-parser.cc	2026-11-03 11:23:44.270317959 +0100
+++ b/gcc/c/c-parser.cc	2026-11-07 19:37:04.059759248 +0100
@@ -13555,6 +13555,8 @@ c_parser_postfix_expression (c_parser *p
 	    break;
 	  }
 	case RID_BUILTIN_BSWAPG:
+	case RID_BUILTIN_BITCOMPRESSG:
+	case RID_BUILTIN_BITEXPANDG:
 	case RID_BUILTIN_BITREVERSEG:
 	case RID_BUILTIN_CLMULG:
 	  {
@@ -13562,6 +13564,7 @@ c_parser_postfix_expression (c_parser *p
 	    location_t close_paren_loc;
 	    internal_fn ifn;
 	    const char *name;
+	    unsigned minarg = 1, maxarg = 1;
 	    switch (c_parser_peek_token (parser)->keyword)
 	      {
 	      case RID_BUILTIN_BSWAPG:
@@ -13575,6 +13578,20 @@ c_parser_postfix_expression (c_parser *p
 	      case RID_BUILTIN_CLMULG:
 		ifn = IFN_CLMUL;
 		name = "__builtin_clmulg";
+		minarg = 2;
+		maxarg = 3;
+		break;
+	      case RID_BUILTIN_BITCOMPRESSG:
+		ifn = IFN_BITCOMPRESS;
+		name = "__builtin_bitcompressg";
+		minarg = 2;
+		maxarg = 2;
+		break;
+	      case RID_BUILTIN_BITEXPANDG:
+		ifn = IFN_BITEXPAND;
+		name = "__builtin_bitexpandg";
+		minarg = 2;
+		maxarg = 2;
 		break;
 	      default:
 		gcc_unreachable ();
@@ -13589,8 +13606,7 @@ c_parser_postfix_expression (c_parser *p
 	      }
 
 	    const unsigned nargs = vec_safe_length (cexpr_list);
-	    if (nargs < (ifn == IFN_CLMUL ? 2 : 1)
-		|| nargs > (ifn == IFN_CLMUL ? 3 : 1))
+	    if (nargs < minarg || nargs > maxarg)
 	      {
 		error_at (loc, "wrong number of arguments to %qs", name);
 		expr.set_error ();
@@ -13647,7 +13663,7 @@ c_parser_postfix_expression (c_parser *p
 		break;
 	      }
 	    arg = c_fully_fold (arg, false, NULL);
-	    if (ifn == IFN_CLMUL)
+	    if (ifn != IFN_BSWAP && ifn != IFN_BITREVERSE)
 	      {
 		arg_p = &(*cexpr_list)[1];
 		*arg_p = convert_lvalue_to_rvalue (loc, *arg_p, true, true);
@@ -13716,7 +13732,13 @@ c_parser_postfix_expression (c_parser *p
 			break;
 		      }
 		  }
-		expr.value = fold_build_builtin_clmulg (loc, arg, arg2, arg3);
+		if (ifn == IFN_CLMUL)
+		  expr.value = fold_build_builtin_clmulg (loc, arg, arg2,
+							  arg3);
+		else
+		  expr.value
+		    = fold_build_builtin_bitcompressg_bitexpandg (loc, ifn,
+								  arg, arg2);
 	      }
 	    else
 	      expr.value = fold_build_builtin_bswapg_bitreverseg (loc, ifn,
--- a/gcc/cp/mangle.cc	2026-11-03 11:23:44.290673170 +0100
+++ b/gcc/cp/mangle.cc	2026-11-04 17:23:29.420702470 +0100
@@ -4133,13 +4133,21 @@ write_expression (tree expr)
 	  else
 	    switch (CALL_EXPR_IFN (expr))
 	      {
-	      /* Calls to __builtin_bswapg/__builtin_bitreverseg/__builtin_clmulg
-		 are parsed as IFN_BSWAP/IFN_BITREVERSE/IFN_CLMUL calls because
-		 we don't have those builtins but instead keywords.  Pretend
-		 they are builtin function template.  */
+	      /* Calls to __builtin_bswapg, __builtin_bitreverseg,
+		 __builtin_clmulg, __builtin_bitcompressg or
+		 __builtin_bitexpandg are parsed as IFN_BSWAP,
+		 IFN_BITREVERSE, IFN_CLMUL, IFN_BITCOMPRESS or IFN_BITEXPAND
+		 calls because we don't have those builtins but instead
+		 keywords.  Pretend they are builtin function template.  */
 	      case IFN_BSWAP:
 		write_source_name (get_identifier ("__builtin_bswapg"));
 		break;
+	      case IFN_BITCOMPRESS:
+		write_source_name (get_identifier ("__builtin_bitcompressg"));
+		break;
+	      case IFN_BITEXPAND:
+		write_source_name (get_identifier ("__builtin_bitexpandg"));
+		break;
 	      case IFN_BITREVERSE:
 		write_source_name (get_identifier ("__builtin_bitreverseg"));
 		break;
--- a/gcc/cp/parser.cc	2026-11-03 11:23:44.284317771 +0100
+++ b/gcc/cp/parser.cc	2026-11-04 17:23:29.417822195 +0100
@@ -8729,6 +8729,8 @@ cp_parser_postfix_expression (cp_parser
     case RID_BUILTIN_OPERATOR_NEW:
     case RID_BUILTIN_OPERATOR_DELETE:
     case RID_BUILTIN_BSWAPG:
+    case RID_BUILTIN_BITCOMPRESSG:
+    case RID_BUILTIN_BITEXPANDG:
     case RID_BUILTIN_BITREVERSEG:
     case RID_BUILTIN_CLMULG:
       {
@@ -8867,6 +8869,18 @@ cp_parser_postfix_expression (cp_parser
 	      = build_x_clmulg (loc, vec, tf_warning_or_error);
 	    break;
 
+	  case RID_BUILTIN_BITCOMPRESSG:
+	    postfix_expression
+	      = build_x_bitcompressg_bitexpandg (loc, IFN_BITCOMPRESS,
+						 vec, tf_warning_or_error);
+	    break;
+
+	  case RID_BUILTIN_BITEXPANDG:
+	    postfix_expression
+	      = build_x_bitcompressg_bitexpandg (loc, IFN_BITEXPAND,
+						 vec, tf_warning_or_error);
+	    break;
+
 	  default:
 	    gcc_unreachable ();
 	  }
--- a/gcc/cp/pt.cc	2026-11-03 11:23:44.289317704 +0100
+++ b/gcc/cp/pt.cc	2026-11-04 17:23:29.413822248 +0100
@@ -22759,6 +22759,17 @@ tsubst_expr (tree t, tree args, tsubst_f
 		break;
 	      }
 
+	    case IFN_BITCOMPRESS:
+	    case IFN_BITEXPAND:
+	      {
+		ret = build_x_bitcompressg_bitexpandg (input_location,
+						       CALL_EXPR_IFN (t),
+						       call_args, complain);
+		if (ret != error_mark_node)
+		  RETURN (ret);
+		break;
+	      }
+
 	    case IFN_ASSUME:
 	      gcc_assert (nargs == 1);
 	      if (vec_safe_length (call_args) != 1)
--- a/gcc/cp/cp-objcp-common.cc	2026-11-03 11:23:44.291013244 +0100
+++ b/gcc/cp/cp-objcp-common.cc	2026-11-04 17:23:29.423888262 +0100
@@ -572,6 +572,8 @@ names_builtin_p (const char *name)
   switch (C_RID_CODE (id))
     {
     case RID_ADDRESSOF:
+    case RID_BUILTIN_BITCOMPRESSG:
+    case RID_BUILTIN_BITEXPANDG:
     case RID_BUILTIN_BITREVERSEG:
     case RID_BUILTIN_BSWAPG:
     case RID_BUILTIN_CLMULG:
--- a/gcc/cp/typeck.cc	2026-11-03 11:23:44.286290766 +0100
+++ b/gcc/cp/typeck.cc	2026-11-04 17:23:29.421408404 +0100
@@ -7341,6 +7341,78 @@ build_x_clmulg (location_t loc, vec<tree
     }
   return exp;
 }
+
+/* Build __builtin_bitcompressg (ARG1, ARG2) or
+   __builtin_bitexpandg (ARG1, ARG2).  */
+tree
+build_x_bitcompressg_bitexpandg (location_t loc, internal_fn ifn,
+				 vec<tree, va_gc> *args,
+				 tsubst_flags_t complain)
+{
+  const char *name
+    = (ifn == IFN_BITCOMPRESS
+       ? "__builtin_bitcompressg" : "__builtin_bitexpandg");
+  if (args->length () != 2)
+    {
+      if (complain & tf_error)
+	error_at (loc, "wrong number of arguments to %qs", name);
+      return error_mark_node;
+    }
+  tree arg1 = (*args)[0];
+  tree arg2 = (*args)[1];
+  if (error_operand_p (arg1) || error_operand_p (arg2))
+    return error_mark_node;
+  if (type_dependent_expression_p (arg1)
+      || type_dependent_expression_p (arg2))
+    {
+      tree exp = build_min_nt_call_vec (NULL, args);
+      CALL_EXPR_IFN (exp) = ifn;
+      return exp;
+    }
+  tree type = TYPE_MAIN_VARIANT (TREE_TYPE (arg1));
+  if (!INTEGRAL_TYPE_P (type))
+    {
+      if (complain & tf_error)
+	error_at (loc, "%qs operand not an integral type", name);
+      return error_mark_node;
+    }
+  if (TREE_CODE (type) == ENUMERAL_TYPE)
+    {
+      if (complain & tf_error)
+	error_at (loc, "argument %u in call to function "
+		       "%qs has enumerated type", 1, name);
+      return error_mark_node;
+    }
+  if (TREE_CODE (type) == BOOLEAN_TYPE)
+    {
+      if (complain & tf_error)
+	error_at (loc, "argument %u in call to function "
+		       "%qs has boolean type", 1, name);
+      return error_mark_node;
+    }
+  if (!TYPE_UNSIGNED (type))
+    {
+      if (complain & tf_error)
+	error_at (loc, "argument 1 in call to function "
+		       "%qs has signed type", name);
+      return error_mark_node;
+    }
+  if (!same_type_p (type, TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
+    {
+      if (complain & tf_error)
+	error_at (loc, "arguments 1 and 2 to function %qs "
+		       "have different types %qT and %qT",
+		  name, type, TYPE_MAIN_VARIANT (TREE_TYPE (arg2)));
+      return error_mark_node;
+    }
+  tree exp = build_call_expr_internal_loc (loc, ifn, type, 2, arg1, arg2);
+  if (processing_template_decl)
+    {
+      exp = build_min_non_dep_call_vec (exp, NULL, args);
+      CALL_EXPR_IFN (exp) = ifn;
+    }
+  return exp;
+}
 
 /* Return a tree for the sum or difference (RESULTCODE says which)
    of pointer PTROP and integer INTOP.  */
--- a/gcc/cp/constexpr.cc	2026-11-03 11:23:44.291926824 +0100
+++ b/gcc/cp/constexpr.cc	2026-11-04 17:23:29.419868480 +0100
@@ -3781,6 +3781,34 @@ cxx_eval_internal_function (const conste
 	return wide_int_to_tree (TREE_TYPE (t), lo);
       }
 
+    case IFN_BITCOMPRESS:
+    case IFN_BITEXPAND:
+      {
+	tree args[2] = {};
+	for (int i = 0; i < call_expr_nargs (t); ++i)
+	  {
+	    args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, i),
+						    vc_prvalue, non_constant_p,
+						    overflow_p, jump_target);
+	    if (*jump_target)
+	      return NULL_TREE;
+	    if (*non_constant_p)
+	      return t;
+	  }
+	location_t loc = cp_expr_loc_or_input_loc (t);
+	if (TREE_CODE (args[0]) != INTEGER_CST
+	    || TREE_CODE (args[1]) != INTEGER_CST)
+	  {
+	    if (!ctx->quiet)
+	      error_at (loc, "call to internal function %qE", t);
+	    *non_constant_p = true;
+	    return t;
+	  }
+	return fold_build_builtin_bitcompressg_bitexpandg (loc,
+							   CALL_EXPR_IFN (t),
+							   args[0], args[1]);
+      }
+
     case IFN_VEC_CONVERT:
       {
 	tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
@@ -12727,6 +12755,8 @@ potential_constant_expression_1 (tree t,
 		case IFN_BSWAP:
 		case IFN_BITREVERSE:
 		case IFN_CLMUL:
+		case IFN_BITCOMPRESS:
+		case IFN_BITEXPAND:
 		  bail = false;
 		  break;
 
--- a/gcc/cp/cp-gimplify.cc	2026-11-03 11:23:44.291252427 +0100
+++ b/gcc/cp/cp-gimplify.cc	2026-11-04 17:23:29.424138246 +0100
@@ -916,6 +916,25 @@ cp_gimplify_expr (tree *expr_p, gimple_s
 		return ret;
 	      }
 	    break;
+	  case IFN_BITCOMPRESS:
+	  case IFN_BITEXPAND:
+	    if (ret == GS_OK)
+	      {
+		location_t loc = EXPR_LOCATION (*expr_p);
+		internal_fn ifn = CALL_EXPR_IFN (*expr_p);
+		tree arg1 = CALL_EXPR_ARG (*expr_p, 0);
+		tree arg2 = CALL_EXPR_ARG (*expr_p, 1);
+		tree r = fold_build_builtin_bitcompressg_bitexpandg (loc, ifn,
+								     arg1,
+								     arg2);
+		if (TREE_CODE (r) == CALL_EXPR
+		    && !CALL_EXPR_FN (r)
+		    && CALL_EXPR_IFN (r) == ifn)
+		  break;
+		*expr_p = r;
+		return ret;
+	      }
+	    break;
 	  default:
 	    break;
 	  }
--- a/gcc/cp/cp-tree.h	2026-11-03 11:23:44.287248215 +0100
+++ b/gcc/cp/cp-tree.h	2026-11-04 17:23:29.423315232 +0100
@@ -9079,6 +9079,9 @@ extern tree build_x_bswapg_bitreverseg
 extern tree build_x_clmulg			(location_t,
 						 vec<tree, va_gc> *,
 						 tsubst_flags_t);
+extern tree build_x_bitcompressg_bitexpandg	(location_t, internal_fn,
+						 vec<tree, va_gc> *,
+						 tsubst_flags_t);
 #define cxx_sizeof(T)  cxx_sizeof_or_alignof_type (input_location, T, SIZEOF_EXPR, false, true)
 extern tree build_simple_component_ref		(tree, tree);
 extern tree build_ptrmemfunc_access_expr	(tree, tree);
--- a/gcc/match.pd	2026-11-03 11:23:44.312317397 +0100
+++ b/gcc/match.pd	2026-11-05 12:54:15.753445922 +0100
@@ -13902,3 +13902,49 @@ and,
    (if (min_prec0 + min_prec1 - 1 <= TYPE_PRECISION (TREE_TYPE (@0)))
     (complex (IFN_CLMUL @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))))
 #endif
+
+(for bitopcall (IFN_BITCOMPRESS IFN_BITEXPAND)
+ (simplify
+  (bitopcall @0 zero_one_valued_p@1)
+   (bit_and @0 @1))
+#if GIMPLE
+ (simplify
+  (bitopcall @0 @1)
+  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
+   (with { tree optype = TREE_TYPE (@0);
+	   unsigned int min_prec1 = TYPE_PRECISION (optype);
+	   int_range_max vr1;
+	   tree ntype = NULL_TREE;
+	   if (gimple_match_range_of_expr (vr1, @1) && !vr1.undefined_p ())
+	     min_prec1 = wi::min_precision (vr1.upper_bound (), UNSIGNED);
+	   if (TYPE_MODE (optype) != BLKmode
+	       && optab_handler (bitopcall == CFN_BITCOMPRESS
+				 ? bitcompress_optab : bitexpand_optab,
+				 TYPE_MODE (optype)) != CODE_FOR_nothing)
+	     min_prec1 = TYPE_PRECISION (optype);
+	   if (min_prec1 < TYPE_PRECISION (optype))
+	     {
+	       ntype = lang_hooks.types.type_for_size (min_prec1, 1);
+	       if (ntype && TYPE_PRECISION (ntype)
+		   >= TYPE_PRECISION (optype))
+		 ntype = NULL_TREE;
+	       else if (ntype == NULL_TREE && BITINT_TYPE_P (optype))
+		 {
+		   struct bitint_info info;
+		   if (targetm.c.bitint_type_info (min_prec1, &info))
+		     {
+		       /* Prefer bit-precise integers with precisions
+			  in multiplies of limb precision.  */
+		       unsigned limb_prec
+			 = GET_MODE_PRECISION (as_a <scalar_int_mode>
+					       (info.limb_mode));
+		       min_prec1 = ROUND_UP (min_prec1, limb_prec);
+		       if (min_prec1 < TYPE_PRECISION (optype))
+			 ntype = build_bitint_type (min_prec1, 1);
+		     }
+		 }
+	     } }
+    (if (ntype)
+     (convert (bitopcall (convert:ntype @0) (convert:ntype @1)))))))
+#endif
+)
--- a/gcc/c-family/c-common.h	2026-11-03 11:23:44.267543832 +0100
+++ b/gcc/c-family/c-common.h	2026-11-04 17:23:29.409367726 +0100
@@ -113,6 +113,7 @@ enum rid
   RID_BUILTIN_HAS_ATTRIBUTE,   RID_BUILTIN_ASSOC_BARRIER,  RID_BUILTIN_STDC,
   RID_BUILTIN_COUNTED_BY_REF,  RID_BUILTIN_BSWAPG,
   RID_BUILTIN_BITREVERSEG,     RID_BUILTIN_CLMULG,
+  RID_BUILTIN_BITCOMPRESSG,    RID_BUILTIN_BITEXPANDG,
   RID_DFLOAT32, RID_DFLOAT64, RID_DFLOAT128, RID_DFLOAT64X,
 
   /* TS 18661-3 keywords, in the same sequence as the TI_* values.  */
--- a/gcc/c-family/c-common.cc	2026-11-03 11:23:44.268275198 +0100
+++ b/gcc/c-family/c-common.cc	2026-11-04 17:23:29.409945673 +0100
@@ -433,6 +433,8 @@ const struct c_common_resword c_common_r
   { "__auto_type",	RID_AUTO_TYPE,	D_CONLY },
   { "__builtin_addressof", RID_ADDRESSOF, D_CXXONLY },
   { "__builtin_assoc_barrier", RID_BUILTIN_ASSOC_BARRIER, 0 },
+  { "__builtin_bitcompressg", RID_BUILTIN_BITCOMPRESSG, 0 },
+  { "__builtin_bitexpandg", RID_BUILTIN_BITEXPANDG, 0 },
   { "__builtin_bitreverseg", RID_BUILTIN_BITREVERSEG, 0 },
   { "__builtin_bit_cast", RID_BUILTIN_BIT_CAST, D_CXXONLY },
   { "__builtin_bswapg", RID_BUILTIN_BSWAPG, 0 },
--- a/gcc/fold-const.cc	2026-11-03 11:23:44.302317530 +0100
+++ b/gcc/fold-const.cc	2026-11-04 17:23:29.431060403 +0100
@@ -14921,6 +14921,8 @@ tree_call_nonnegative_p (tree type, comb
     CASE_CFN_BSWAP:
     CASE_CFN_BITREVERSE:
     case CFN_CLMUL:
+    case CFN_BITCOMPRESS:
+    case CFN_BITEXPAND:
       /* Always true.  */
       return true;
 
--- a/gcc/wide-int.cc	2026-11-03 11:23:44.304987184 +0100
+++ b/gcc/wide-int.cc	2026-11-04 18:04:22.936685549 +0100
@@ -2466,6 +2466,65 @@ wi::clmul (const wide_int_ref &x, const
   return r;
 }
 
+/* Compute bit compression of X using mask M with precision PREC.
+   Based on "Compress, or Generalized Extract" in Hacker's Delight.  */
+
+wide_int
+wi::bit_compress (const wide_int_ref &x, const wide_int_ref &m,
+		  unsigned int prec)
+{
+  wide_int r = wi::bit_and (x, m);
+  wide_int mk = wi::lshift (wi::bit_not (m), 1);
+  wide_int mm = wide_int::from (m, prec, UNSIGNED);
+  for (unsigned int i = 1; i < prec; i <<= 1)
+    {
+      wide_int mp = mk;
+      for (unsigned int j = 1; j < prec; j <<= 1)
+	mp = wi::bit_xor (mp, wi::lshift (mp, j));
+      wide_int mv = wi::bit_and (mp, mm);
+      mm = wi::bit_or (wi::bit_xor (mm, mv), wi::lrshift (mv, i));
+      wide_int t = wi::bit_and (r, mv);
+      r = wi::bit_or (wi::bit_xor (r, t), wi::lrshift (t, i));
+      mk = wi::bit_and_not (mk, mp);
+    }
+  return r;
+}
+
+/* Compute bit expansion of X using mask M with precision PREC.
+   Based on "Expand, or Generalized Insert" in Hacker's Delight.  */
+
+wide_int
+wi::bit_expand (const wide_int_ref &x, const wide_int_ref &m,
+		unsigned int prec)
+{
+  if (prec == 1)
+    return wi::bit_and (x, m);
+  unsigned log2_prec = ceil_log2 (prec);
+  wide_int m0 = wide_int::from (m, prec, UNSIGNED);
+  wide_int mk = wi::lshift (wi::bit_not (m), 1);
+  wide_int mm = wide_int::from (m, prec, UNSIGNED);
+  wide_int r = wide_int::from (x, prec, UNSIGNED);
+  auto_vec<wide_int, 16> vec (log2_prec);
+  for (unsigned int i = 0; i < log2_prec; ++i)
+    {
+      wide_int mp = mk;
+      for (unsigned int j = 1; j < prec; j <<= 1)
+	mp = wi::bit_xor (mp, wi::lshift (mp, j));
+     wide_int mv = wi::bit_and (mp, mm);
+     vec.safe_push (mv);
+      mm = wi::bit_or (wi::bit_xor (mm, mv), wi::lrshift (mv, 1U << i));
+      mk = wi::bit_and_not (mk, mp);
+    }
+  for (int i = log2_prec - 1; i >= 0; --i)
+    {
+      wide_int mv = vec.last ();
+      vec.pop ();
+      wide_int t = wi::lshift (r, 1U << i);
+      r = wi::bit_or (wi::bit_and_not (r, mv), wi::bit_and (t, mv));
+    }
+  return wi::bit_and (r, m0);
+}
+
 /*
  * Private utilities.
  */
--- a/gcc/builtins.h	2026-11-03 11:23:44.299544286 +0100
+++ b/gcc/builtins.h	2026-11-04 17:23:29.424429967 +0100
@@ -143,6 +143,9 @@ extern tree fold_builtin_constant_p (tre
 extern tree fold_build_builtin_bswapg_bitreverseg (location_t, internal_fn,
 						   tree);
 extern tree fold_build_builtin_clmulg (location_t, tree, tree, tree);
+extern tree fold_build_builtin_bitcompressg_bitexpandg (location_t,
+							internal_fn,
+							tree, tree);
 extern bool avoid_folding_inline_builtin (tree);
 extern tree fold_call_expr (location_t, tree, bool);
 extern tree fold_builtin_call_array (location_t, tree, tree, int, tree *);
--- a/gcc/gimple-lower-bitint.cc	2026-11-03 11:23:44.309317437 +0100
+++ b/gcc/gimple-lower-bitint.cc	2026-11-05 11:50:01.669940651 +0100
@@ -3350,7 +3350,9 @@ bitint_large_huge::lower_mergeable_stmt
   if (gimple_store_p (stmt))
     {
       unlink_stmt_vdef (stmt);
-      release_ssa_name (gimple_vdef (stmt));
+      if (tree vdef = gimple_vdef (stmt))
+	if (TREE_CODE (vdef) == SSA_NAME)
+	  release_ssa_name (vdef);
       gsi_remove (&m_gsi, true);
     }
   if (eq_p)
@@ -6837,7 +6839,9 @@ bitint_large_huge::lower_stmt (gimple *s
 	      handled:
 		m_gsi = gsi_for_stmt (stmt);
 		unlink_stmt_vdef (stmt);
-		release_ssa_name (gimple_vdef (stmt));
+		if (tree vdef = gimple_vdef (stmt))
+		  if (TREE_CODE (vdef) == SSA_NAME)
+		    release_ssa_name (vdef);
 		gsi_remove (&m_gsi, true);
 		return;
 	      case MULT_EXPR:
@@ -7476,6 +7480,282 @@ arith_overflow_arg_kind (gimple *stmt)
   return ret;
 }
 
+/* Lower a call to __builtin_bitcompressg (ARG0, ARG1) or
+   __builtin_bitexpandg (ARG0, ARG1) for large/huge _BitInt types.  */
+
+static void
+lower_bitcompress_bitexpand (gimple *stmt)
+{
+  location_t loc = gimple_location (stmt);
+  tree arg0 = gimple_call_arg (stmt, 0);
+  tree arg1 = gimple_call_arg (stmt, 1);
+  tree lhs = gimple_call_lhs (stmt);
+  gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
+  internal_fn ifn = gimple_call_internal_fn (stmt);
+  gcc_assert (ifn == IFN_BITCOMPRESS || ifn == IFN_BITEXPAND);
+  tree type = TREE_TYPE (arg0);
+  unsigned prec = TYPE_PRECISION (type);
+  unsigned log2prec = ceil_log2 (prec);
+  if (!lhs)
+    {
+      gsi_remove (&gsi, true);
+      return;
+    }
+  calculate_dominance_info (CDI_DOMINATORS);
+  /* Based on "Compress, or Generalized Extract" in Hacker's Delight:
+     T
+     bitcompressg (T x, T m)
+     {
+       int prec = __builtin_popcountg ((T) ~(T) 0);
+       x &= m;
+       T mk = ~m << 1;
+       for (int i = 1; i < prec; i <<= 1)
+	 {
+	   T mp = mk;
+	   for (int j = 1; j < prec; j <<= 1)
+	     mp ^= mp << j;
+	   T mv = mp & m;
+	   m = (m ^ mv) | (mv >> i);
+	   T t = x & mv;
+	   x = (x ^ t) | (t >> i);
+	   mk = mk & ~mp;
+	 }
+       return x;
+     }
+     and based on "Expand, or Generalized Insert" in Hacker's Delight:
+     T
+     bitexpandg (T x, T m)
+     {
+       int prec = __builtin_popcountg ((T) ~(T) 0);
+       int log2prec = ceil_log2 (prec);
+       T m0 = m;
+       T mk = ~m << 1;
+       T array[log2prec];
+       for (int i = 0; i < log2prec; i++)
+	 {
+	   T mp = mk;
+	   for (int j = 1; j < prec; j <<= 1)
+	     mp ^= mp << j;
+	   T mv = mp & m;
+	   array[i] = mv;
+	   m = (m ^ mv) | (mv >> (1 << i));
+	   mk = mk & ~mp;
+	 }
+       for (int i = log2prec - 1; i >= 0; i--)
+	 {
+	   T mv = array[i];
+	   T t = x << (((T) 1) << i);
+	   x = (x & ~mv) | (t & mv);
+	 }
+       return x & m0;
+     }  */
+  tree x = arg0;
+  tree m = arg1;
+  gimple *g;
+  if (TREE_CODE (x) != SSA_NAME || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (x))
+    {
+      x = make_ssa_name (type);
+      g = gimple_build_assign (x, arg0);
+      gimple_set_location (g, loc);
+      gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+    }
+  if (TREE_CODE (m) != SSA_NAME || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (m))
+    {
+      m = make_ssa_name (type);
+      g = gimple_build_assign (m, arg1);
+      gimple_set_location (g, loc);
+      gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+    }
+  tree m0 = m;
+  tree array = NULL_TREE;
+  if (ifn == IFN_BITCOMPRESS)
+    {
+      g = gimple_build_assign (make_ssa_name (type),
+			       BIT_AND_EXPR, x, m);
+      gimple_set_location (g, loc);
+      gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+      x = gimple_assign_lhs (g);
+    }
+  else
+    {
+      tree atype = build_array_type_nelts (type, log2prec);
+      array = create_tmp_var (atype);
+    }
+  g = gimple_build_assign (make_ssa_name (type), BIT_NOT_EXPR, m);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  tree mk = make_ssa_name (type);
+  g = gimple_build_assign (mk, LSHIFT_EXPR, gimple_assign_lhs (g),
+			   build_int_cst (unsigned_type_node, 1));
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  edge preheader_edge, latch_edge, exit_edge;
+  auto create_loop_after
+    = [] (basic_block bb, gimple *g, edge *e1, edge *e2, edge *e3) {
+    *e1 = split_block (bb, g);
+    *e2 = split_block ((*e1)->dest, (gimple *) NULL);
+    *e3 = make_edge ((*e1)->dest, (*e1)->dest, EDGE_TRUE_VALUE);
+    (*e3)->probability = profile_probability::very_unlikely ();
+    (*e2)->flags = EDGE_FALSE_VALUE;
+    (*e2)->probability = (*e3)->probability.invert ();
+    gimple_stmt_iterator gsi = gsi_after_labels ((*e1)->dest);
+    class loop *loop = alloc_loop ();
+    loop->header = (*e1)->dest;
+    add_loop (loop, (*e1)->src->loop_father);
+    return gsi;
+  };
+  auto add_loop_phi = [] (tree init, tree *next, edge e1, edge e3) {
+    tree res = make_ssa_name (TREE_TYPE (init));
+    gphi *phi = create_phi_node (res, e1->dest);
+    add_phi_arg (phi, init, e1, UNKNOWN_LOCATION);
+    *next = make_ssa_name (TREE_TYPE (init));
+    add_phi_arg (phi, *next, e3, UNKNOWN_LOCATION);
+    return res;
+  };
+  gsi = create_loop_after (gimple_bb (g), g, &preheader_edge,
+			   &exit_edge, &latch_edge);
+  tree i_next, m_next, mk_next, x_next = NULL_TREE;
+  tree i = add_loop_phi (build_int_cst (integer_type_node,
+					ifn == IFN_BITCOMPRESS),
+			 &i_next, preheader_edge, latch_edge);
+  m = add_loop_phi (m, &m_next, preheader_edge, latch_edge);
+  mk = add_loop_phi (mk, &mk_next, preheader_edge, latch_edge);
+  if (ifn == IFN_BITCOMPRESS)
+    x = add_loop_phi (x, &x_next, preheader_edge, latch_edge);
+  gsi = create_loop_after (gsi_bb (gsi), NULL, &preheader_edge,
+			   &exit_edge, &latch_edge);
+  tree j_next, mp_next;
+  tree j = add_loop_phi (build_int_cst (integer_type_node, 1), &j_next,
+			 preheader_edge, latch_edge);
+  tree mp = add_loop_phi (mk, &mp_next, preheader_edge, latch_edge);
+  g = gimple_build_assign (make_ssa_name (type), LSHIFT_EXPR, mp, j);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  g = gimple_build_assign (mp_next, BIT_XOR_EXPR, mp, gimple_assign_lhs (g));
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  g = gimple_build_assign (j_next, PLUS_EXPR, j, j);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  g = gimple_build_cond (LT_EXPR, j_next,
+			 build_int_cst (integer_type_node, prec),
+			 NULL_TREE, NULL_TREE);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  gsi = gsi_after_labels (exit_edge->dest);
+  mp = mp_next;
+  tree mv = make_ssa_name (type);
+  g = gimple_build_assign (mv, BIT_AND_EXPR, mp, m);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  tree shiftcnt = i;
+  if (ifn == IFN_BITEXPAND)
+    {
+      tree aref = build4 (ARRAY_REF, type, array, i, NULL_TREE, NULL_TREE);
+      g = gimple_build_assign (aref, mv);
+      gimple_set_location (g, loc);
+      gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+      shiftcnt = make_ssa_name (integer_type_node);
+      g = gimple_build_assign (shiftcnt, LSHIFT_EXPR, integer_one_node, i);
+      gimple_set_location (g, loc);
+      gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+    }
+  tree tem = make_ssa_name (type);
+  g = gimple_build_assign (tem, BIT_XOR_EXPR, m, mv);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  g = gimple_build_assign (make_ssa_name (type), RSHIFT_EXPR, mv, shiftcnt);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  g = gimple_build_assign (m_next, BIT_IOR_EXPR, tem, gimple_assign_lhs (g));
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  if (ifn == IFN_BITCOMPRESS)
+    {
+      tree t = make_ssa_name (type);
+      g = gimple_build_assign (t, BIT_AND_EXPR, x, mv);
+      gimple_set_location (g, loc);
+      gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+      tem = make_ssa_name (type);
+      g = gimple_build_assign (tem, BIT_XOR_EXPR, x, t);
+      gimple_set_location (g, loc);
+      gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+      g = gimple_build_assign (make_ssa_name (type), RSHIFT_EXPR, t, shiftcnt);
+      gimple_set_location (g, loc);
+      gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+      g = gimple_build_assign (x_next, BIT_IOR_EXPR, tem, gimple_assign_lhs (g));
+      gimple_set_location (g, loc);
+      gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+    }
+  g = gimple_build_assign (make_ssa_name (type), BIT_NOT_EXPR, mp);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  g = gimple_build_assign (mk_next, BIT_AND_EXPR, mk, gimple_assign_lhs (g));
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  g = gimple_build_assign (i_next, PLUS_EXPR, i,
+			   ifn == IFN_BITCOMPRESS ? i : integer_one_node);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  g = gimple_build_cond (LT_EXPR, i_next,
+			 build_int_cst (integer_type_node,
+					ifn == IFN_BITCOMPRESS
+					? prec : log2prec),
+			 NULL_TREE, NULL_TREE);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  gsi = gsi_for_stmt (stmt);
+  if (ifn == IFN_BITCOMPRESS)
+    {
+      g = gimple_build_assign (lhs, x_next);
+      gimple_set_location (g, loc);
+      gsi_replace (&gsi, g, true);
+      return;
+    }
+  gsi = create_loop_after (gsi_bb (gsi), NULL, &preheader_edge,
+			   &exit_edge, &latch_edge);
+  i = add_loop_phi (build_int_cst (integer_type_node, log2prec - 1),
+		    &i_next, preheader_edge, latch_edge);
+  x = add_loop_phi (x, &x_next, preheader_edge, latch_edge);
+  tree aref = build4 (ARRAY_REF, type, array, i, NULL_TREE, NULL_TREE);
+  mv = make_ssa_name (type);
+  g = gimple_build_assign (mv, aref);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  shiftcnt = make_ssa_name (integer_type_node);
+  g = gimple_build_assign (shiftcnt, LSHIFT_EXPR, integer_one_node, i);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  tree t = make_ssa_name (type);
+  g = gimple_build_assign (t, LSHIFT_EXPR, x, shiftcnt);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  g = gimple_build_assign (make_ssa_name (type), BIT_NOT_EXPR, mv);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  tem = make_ssa_name (type);
+  g = gimple_build_assign (tem, BIT_AND_EXPR, x, gimple_assign_lhs (g));
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  g = gimple_build_assign (make_ssa_name (type), BIT_AND_EXPR, t, mv);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  g = gimple_build_assign (x_next, BIT_IOR_EXPR, tem, gimple_assign_lhs (g));
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  g = gimple_build_assign (i_next, PLUS_EXPR, i, integer_minus_one_node);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  g = gimple_build_cond (GE_EXPR, i_next, integer_zero_node,
+			 NULL_TREE, NULL_TREE);
+  gimple_set_location (g, loc);
+  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  gsi = gsi_for_stmt (stmt);
+  g = gimple_build_assign (lhs, BIT_AND_EXPR, x_next, m0);
+  gimple_set_location (g, loc);
+  gsi_replace (&gsi, g, true);
+}
+
 /* Entry point for _BitInt(N) operation lowering during optimization.  */
 
 static unsigned int
@@ -7884,6 +8164,12 @@ gimple_lower_bitint (void)
 	      gimple_set_location (g, loc);
 	      gsi_replace (&gsi, g, true);
 	    }
+	  else if (gimple_call_internal_p (stmt, IFN_BITCOMPRESS)
+		   || gimple_call_internal_p (stmt, IFN_BITEXPAND))
+	    {
+	      first_large_huge = 0;
+	      lower_bitcompress_bitexpand (stmt);
+	    }
 	  else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (s)
 		   && is_gimple_call (stmt)
 		   && gimple_call_internal_p (stmt))
--- a/gcc/testsuite/c-c++-common/builtin-bitexpandg-2.c	2026-11-04 17:23:29.432186955 +0100
+++ b/gcc/testsuite/c-c++-common/builtin-bitexpandg-2.c	2026-11-04 17:23:29.432186955 +0100
@@ -0,0 +1,36 @@
+/* Test __builtin_bitexpandg.  */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+int
+main ()
+{
+  unsigned int a;
+  __builtin_bitexpandg ();			/* { dg-error "wrong number of arguments to '__builtin_bitexpandg'" } */
+  __builtin_bitexpandg (1U);			/* { dg-error "wrong number of arguments to '__builtin_bitexpandg'" } */
+  __builtin_bitexpandg (1U, 1U, &a);		/* { dg-error "wrong number of arguments to '__builtin_bitexpandg'" } */
+  __builtin_bitexpandg (1U, 1U, &a, 1U);	/* { dg-error "wrong number of arguments to '__builtin_bitexpandg'" } */
+  __builtin_bitexpandg (1.0, 1.0);		/* { dg-error "'__builtin_bitexpandg' operand not an integral type" } */
+  __builtin_bitexpandg (1.0f, 1.0f);		/* { dg-error "'__builtin_bitexpandg' operand not an integral type" } */
+  __builtin_bitexpandg (1.0L, 1.0L);		/* { dg-error "'__builtin_bitexpandg' operand not an integral type" } */
+  struct S { int a; };
+  __builtin_bitexpandg ((struct S) { 42 }, (struct S) { 43 }); /* { dg-error "'__builtin_bitexpandg' operand not an integral type" } */
+  enum E { E0, E1 } e;
+  __builtin_bitexpandg (E1, E1);		/* { dg-error "argument 1 in call to function '__builtin_bitexpandg' has signed type" "" { target c } } */
+						/* { dg-error "argument 1 in call to function '__builtin_bitexpandg' has enumerated type" "" { target c++ } .-1 } */
+  __builtin_bitexpandg (e, e);		/* { dg-error "argument 1 in call to function '__builtin_bitexpandg' has enumerated type" } */
+  __builtin_bitexpandg ((enum E) 1, (enum E) 1); /* { dg-error "argument 1 in call to function '__builtin_bitexpandg' has enumerated type" } */
+#if !defined(__cplusplus) || __cplusplus >=  201103L
+  enum F : int { F0, F1 } f;
+  __builtin_bitexpandg (F1, F1);		/* { dg-error "argument 1 in call to function '__builtin_bitexpandg' has enumerated type" "" { target { c || c++11 } } } */
+  __builtin_bitexpandg (f, f);		/* { dg-error "argument 1 in call to function '__builtin_bitexpandg' has enumerated type" "" { target { c || c++11 } } } */
+  __builtin_bitexpandg ((enum F) 1, (enum F) 1); /* { dg-error "argument 1 in call to function '__builtin_bitexpandg' has enumerated type" "" { target { c || c++11 } } } */
+#endif
+  __builtin_bitexpandg (false, true);		/* { dg-error "argument 1 in call to function '__builtin_bitexpandg' has boolean type" } */
+  __builtin_bitexpandg (1, 1);		/* { dg-error "argument 1 in call to function '__builtin_bitexpandg' has signed type" } */
+  __builtin_bitexpandg ((char) 1, (char) 1);	/* { dg-error "argument 1 in call to function '__builtin_bitexpandg' has signed type" "" { target { ! unsigned_char } } } */
+  __builtin_bitexpandg (1UL, 1ULL);		/* { dg-error "arguments 1 and 2 to function '__builtin_bitexpandg' have different types 'long unsigned int' and 'long long unsigned int'" } */
+  __builtin_bitexpandg (1U, 1.0);		/* { dg-error "arguments 1 and 2 to function '__builtin_bitexpandg' have different types 'unsigned int' and 'double'" } */
+  __builtin_bitexpandg ((unsigned char) 1, (char) 1); /* { dg-error "arguments 1 and 2 to function '__builtin_bitexpandg' have different types 'unsigned char' and 'char'" } */
+  __builtin_bitexpandg ((unsigned char) 1, true); /* { dg-error "arguments 1 and 2 to function '__builtin_bitexpandg' have different types 'unsigned char' and 'bool'" } */
+}
--- a/gcc/testsuite/c-c++-common/builtin-bitexpandg-1.c	2026-11-04 17:23:29.432634935 +0100
+++ b/gcc/testsuite/c-c++-common/builtin-bitexpandg-1.c	2026-11-05 12:16:34.531733128 +0100
@@ -0,0 +1,380 @@
+/* Test __builtin_bitexpandg.  */
+/* { dg-do run { target { c || c++11 } } } */
+/* { dg-options "-O2" } */
+
+#if !__has_builtin(__builtin_bitexpandg)
+#error __builtin_bitexpandg unsupported
+#endif
+
+static_assert (__builtin_bitexpandg ((unsigned char) 0, (unsigned char) 0) == (unsigned char) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned char) 0x123456789abcdefULL, (unsigned char) 0) == (unsigned char) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned char) 0x123456789abcdefULL, (unsigned char) ~(unsigned char) 0) == (unsigned char) 0x123456789abcdefULL, "");
+static_assert (__builtin_bitexpandg ((unsigned char) 0, (unsigned char) 0x123456789abcdefULL) == (unsigned char) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned char) ~(unsigned char) 0, (unsigned char) ~(unsigned char) 0) == (unsigned char) ~(unsigned char) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned char) ((((unsigned char) 1) << __builtin_popcountg ((unsigned char) 0x123456789abcdefULL)) - 1), (unsigned char) 0x123456789abcdefULL) == (unsigned char) 0x123456789abcdefULL, "");
+static_assert (__builtin_bitexpandg ((unsigned char) 0xff, (unsigned char) 0xff) == 0xff, "");
+static_assert (__builtin_bitexpandg ((unsigned char) 0x0f, (unsigned char) 0xff) == 0x0f, "");
+static_assert (__builtin_bitexpandg ((unsigned char) 0x0f, (unsigned char) 0xf0) == 0xf0, "");
+static_assert (__builtin_bitexpandg ((unsigned char) 0xaaaaaaaaaaaaaaaaULL, (unsigned char) 0xccccccccccccccccULL) == (unsigned char) 0x8888888888888888ULL, "");
+static_assert (__builtin_bitexpandg ((unsigned char) 0x0f0f0f0f0f0f0f0fULL, (unsigned char) 0xaaaaaaaaaaaaaaaaULL) == (unsigned char) 0x00aa00aa00aa00aaULL, "");
+static_assert (__builtin_bitexpandg ((unsigned char) 0xd4, (unsigned char) 0xbf) == 0x94, "");
+static_assert (__builtin_bitexpandg ((unsigned char) 0x66, (unsigned char) 0x1d) == 0x0c, "");
+static_assert (__builtin_bitexpandg ((unsigned short) 0, (unsigned short) 0) == (unsigned short) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned short) 0x123456789abcdefULL, (unsigned short) 0) == (unsigned short) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned short) 0x123456789abcdefULL, (unsigned short) ~(unsigned short) 0) == (unsigned short) 0x123456789abcdefULL, "");
+static_assert (__builtin_bitexpandg ((unsigned short) 0, (unsigned short) 0x123456789abcdefULL) == (unsigned short) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned short) ~(unsigned short) 0, (unsigned short) ~(unsigned short) 0) == (unsigned short) ~(unsigned short) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned short) ((((unsigned short) 1) << __builtin_popcountg ((unsigned short) 0x123456789abcdefULL)) - 1), (unsigned short) 0x123456789abcdefULL) == (unsigned short) 0x123456789abcdefULL, "");
+#if __CHAR_BIT__ * __SIZEOF_SHORT__ >= 16
+static_assert (__builtin_bitexpandg ((unsigned short) 0x4646464646464646ULL, (unsigned short) 0x5555555555555555ULL) == (unsigned short) 0x1014101410141014ULL, "");
+static_assert (__builtin_bitexpandg ((unsigned short) 0x1414141414141414ULL, (unsigned short) 0xaaaaaaaaaaaaaaaaULL) == (unsigned short) 0x0220022002200220ULL, "");
+#endif
+static_assert (__builtin_bitexpandg ((unsigned short) 0xff, (unsigned short) 0xff) == 0xff, "");
+static_assert (__builtin_bitexpandg ((unsigned short) 0x0f, (unsigned short) 0xff) == 0x0f, "");
+static_assert (__builtin_bitexpandg ((unsigned short) 0x0f, (unsigned short) 0xf0) == 0xf0, "");
+static_assert (__builtin_bitexpandg ((unsigned short) 0xaaaaaaaaaaaaaaaaULL, (unsigned short) 0xccccccccccccccccULL) == (unsigned short) 0x8888888888888888ULL, "");
+static_assert (__builtin_bitexpandg ((unsigned short) 0x0f0f0f0f0f0f0f0fULL, (unsigned short) 0xaaaaaaaaaaaaaaaaULL) == (unsigned short) 0x00aa00aa00aa00aaULL, "");
+static_assert (__builtin_bitexpandg ((unsigned short) 0xd4, (unsigned short) 0xbf) == 0x94, "");
+static_assert (__builtin_bitexpandg ((unsigned short) 0x66, (unsigned short) 0x1d) == 0x0c, "");
+#if __CHAR_BIT__ * __SIZEOF_SHORT__ >= 16
+static_assert (__builtin_bitexpandg ((unsigned short) 0xebd4U, (unsigned short) 0x93bfU) == (unsigned short) 0x1394, "");
+static_assert (__builtin_bitexpandg ((unsigned short) 0xa166U, (unsigned short) 0x001dU) == (unsigned short) 0xc, "");
+#endif
+static_assert (__builtin_bitexpandg ((unsigned int) 0, (unsigned int) 0) == (unsigned int) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned int) 0x123456789abcdefULL, (unsigned int) 0) == (unsigned int) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned int) 0x123456789abcdefULL, (unsigned int) ~(unsigned int) 0) == (unsigned int) 0x123456789abcdefULL, "");
+static_assert (__builtin_bitexpandg ((unsigned int) 0, (unsigned int) 0x123456789abcdefULL) == (unsigned int) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned int) ~(unsigned int) 0, (unsigned int) ~(unsigned int) 0) == (unsigned int) ~(unsigned int) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned int) ((((unsigned int) 1) << __builtin_popcountg ((unsigned int) 0x123456789abcdefULL)) - 1), (unsigned int) 0x123456789abcdefULL) == (unsigned int) 0x123456789abcdefULL, "");
+#if __CHAR_BIT__ * __SIZEOF_INT__ >= 16
+static_assert (__builtin_bitexpandg ((unsigned int) 0x4646464646464646ULL, (unsigned int) 0x5555555555555555ULL) == (unsigned int) 0x1014101410141014ULL, "");
+static_assert (__builtin_bitexpandg ((unsigned int) 0x1414141414141414ULL, (unsigned int) 0xaaaaaaaaaaaaaaaaULL) == (unsigned int) 0x0220022002200220ULL, "");
+#endif
+static_assert (__builtin_bitexpandg ((unsigned int) 0xff, (unsigned int) 0xff) == 0xff, "");
+static_assert (__builtin_bitexpandg ((unsigned int) 0x0f, (unsigned int) 0xff) == 0x0f, "");
+static_assert (__builtin_bitexpandg ((unsigned int) 0x0f, (unsigned int) 0xf0) == 0xf0, "");
+static_assert (__builtin_bitexpandg ((unsigned int) 0xaaaaaaaaaaaaaaaaULL, (unsigned int) 0xccccccccccccccccULL) == (unsigned int) 0x8888888888888888ULL, "");
+static_assert (__builtin_bitexpandg ((unsigned int) 0x0f0f0f0f0f0f0f0fULL, (unsigned int) 0xaaaaaaaaaaaaaaaaULL) == (unsigned int) 0x00aa00aa00aa00aaULL, "");
+static_assert (__builtin_bitexpandg ((unsigned int) 0xd4, (unsigned int) 0xbf) == 0x94, "");
+static_assert (__builtin_bitexpandg ((unsigned int) 0x66, (unsigned int) 0x1d) == 0x0c, "");
+#if __CHAR_BIT__ * __SIZEOF_INT__ >= 16
+static_assert (__builtin_bitexpandg ((unsigned int) 0xebd4U, (unsigned int) 0x93bfU) == (unsigned int) 0x1394, "");
+static_assert (__builtin_bitexpandg ((unsigned int) 0xa166U, (unsigned int) 0x001dU) == (unsigned int) 0xc, "");
+#endif
+#if __CHAR_BIT__ * __SIZEOF_INT__ >= 32
+static_assert (__builtin_bitexpandg ((unsigned int) 0x9daeebd4UL, (unsigned int) 0xad8793bfUL) == (unsigned int) 0xa9851394UL, "");
+static_assert (__builtin_bitexpandg ((unsigned int) 0x979ba166UL, (unsigned int) 0x54ca001dUL) == (unsigned int) 0x448000cUL, "");
+#endif
+static_assert (__builtin_bitexpandg ((unsigned long) 0, (unsigned long) 0) == (unsigned long) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned long) 0x123456789abcdefULL, (unsigned long) 0) == (unsigned long) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned long) 0x123456789abcdefULL, (unsigned long) ~(unsigned long) 0) == (unsigned long) 0x123456789abcdefULL, "");
+static_assert (__builtin_bitexpandg ((unsigned long) 0, (unsigned long) 0x123456789abcdefULL) == (unsigned long) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned long) ~(unsigned long) 0, (unsigned long) ~(unsigned long) 0) == (unsigned long) ~(unsigned long) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned long) ((((unsigned long) 1) << __builtin_popcountg ((unsigned long) 0x123456789abcdefULL)) - 1), (unsigned long) 0x123456789abcdefULL) == (unsigned long) 0x123456789abcdefULL, "");
+#if __CHAR_BIT__ * __SIZEOF_LONG__ >= 16
+static_assert (__builtin_bitexpandg ((unsigned long) 0x4646464646464646ULL, (unsigned long) 0x5555555555555555ULL) == (unsigned long) 0x1014101410141014ULL, "");
+static_assert (__builtin_bitexpandg ((unsigned long) 0x1414141414141414ULL, (unsigned long) 0xaaaaaaaaaaaaaaaaULL) == (unsigned long) 0x0220022002200220ULL, "");
+#endif
+static_assert (__builtin_bitexpandg ((unsigned long) 0xff, (unsigned long) 0xff) == 0xff, "");
+static_assert (__builtin_bitexpandg ((unsigned long) 0x0f, (unsigned long) 0xff) == 0x0f, "");
+static_assert (__builtin_bitexpandg ((unsigned long) 0x0f, (unsigned long) 0xf0) == 0xf0, "");
+static_assert (__builtin_bitexpandg ((unsigned long) 0xaaaaaaaaaaaaaaaaULL, (unsigned long) 0xccccccccccccccccULL) == (unsigned long) 0x8888888888888888ULL, "");
+static_assert (__builtin_bitexpandg ((unsigned long) 0x0f0f0f0f0f0f0f0fULL, (unsigned long) 0xaaaaaaaaaaaaaaaaULL) == (unsigned long) 0x00aa00aa00aa00aaULL, "");
+static_assert (__builtin_bitexpandg ((unsigned long) 0xd4, (unsigned long) 0xbf) == 0x94, "");
+static_assert (__builtin_bitexpandg ((unsigned long) 0x66, (unsigned long) 0x1d) == 0x0c, "");
+#if __CHAR_BIT__ * __SIZEOF_LONG__ >= 16
+static_assert (__builtin_bitexpandg ((unsigned long) 0xebd4U, (unsigned long) 0x93bfU) == (unsigned long) 0x1394, "");
+static_assert (__builtin_bitexpandg ((unsigned long) 0xa166U, (unsigned long) 0x001dU) == (unsigned long) 0xc, "");
+#endif
+#if __CHAR_BIT__ * __SIZEOF_LONG__ >= 32
+static_assert (__builtin_bitexpandg ((unsigned long) 0x9daeebd4UL, (unsigned long) 0xad8793bfUL) == (unsigned long) 0xa9851394UL, "");
+static_assert (__builtin_bitexpandg ((unsigned long) 0x979ba166UL, (unsigned long) 0x54ca001dUL) == (unsigned long) 0x448000cUL, "");
+#endif
+#if __CHAR_BIT__ * __SIZEOF_LONG__ >= 64
+static_assert (__builtin_bitexpandg ((unsigned long) 0x5bd30f069daeebd4ULL, (unsigned long) 0x64f9a89bad8793bfULL) == (unsigned long) 0x4a1a092a9851394ULL, "");
+static_assert (__builtin_bitexpandg ((unsigned long) 0xb5672499979ba166ULL, (unsigned long) 0xd315782354ca001dULL) == (unsigned long) 0xd01470200448000cULL, "");
+#endif
+static_assert (__builtin_bitexpandg ((unsigned long long) 0, (unsigned long long) 0) == (unsigned long long) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned long long) 0x123456789abcdefULL, (unsigned long long) 0) == (unsigned long long) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned long long) 0x123456789abcdefULL, (unsigned long long) ~(unsigned long long) 0) == (unsigned long long) 0x123456789abcdefULL, "");
+static_assert (__builtin_bitexpandg ((unsigned long long) 0, (unsigned long long) 0x123456789abcdefULL) == (unsigned long long) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned long long) ~(unsigned long long) 0, (unsigned long long) ~(unsigned long long) 0) == (unsigned long long) ~(unsigned long long) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned long long) ((((unsigned long long) 1) << __builtin_popcountg ((unsigned long long) 0x123456789abcdefULL)) - 1), (unsigned long long) 0x123456789abcdefULL) == (unsigned long long) 0x123456789abcdefULL, "");
+#if __CHAR_BIT__ * __SIZEOF_LONG_LONG__ >= 16
+static_assert (__builtin_bitexpandg ((unsigned long long) 0x4646464646464646ULL, (unsigned long long) 0x5555555555555555ULL) == (unsigned long long) 0x1014101410141014ULL, "");
+static_assert (__builtin_bitexpandg ((unsigned long long) 0x1414141414141414ULL, (unsigned long long) 0xaaaaaaaaaaaaaaaaULL) == (unsigned long long) 0x0220022002200220ULL, "");
+#endif
+static_assert (__builtin_bitexpandg ((unsigned long long) 0xff, (unsigned long long) 0xff) == 0xff, "");
+static_assert (__builtin_bitexpandg ((unsigned long long) 0x0f, (unsigned long long) 0xff) == 0x0f, "");
+static_assert (__builtin_bitexpandg ((unsigned long long) 0x0f, (unsigned long long) 0xf0) == 0xf0, "");
+static_assert (__builtin_bitexpandg ((unsigned long long) 0xaaaaaaaaaaaaaaaaULL, (unsigned long long) 0xccccccccccccccccULL) == (unsigned long long) 0x8888888888888888ULL, "");
+static_assert (__builtin_bitexpandg ((unsigned long long) 0x0f0f0f0f0f0f0f0fULL, (unsigned long long) 0xaaaaaaaaaaaaaaaaULL) == (unsigned long long) 0x00aa00aa00aa00aaULL, "");
+static_assert (__builtin_bitexpandg ((unsigned long long) 0xd4, (unsigned long long) 0xbf) == 0x94, "");
+static_assert (__builtin_bitexpandg ((unsigned long long) 0x66, (unsigned long long) 0x1d) == 0x0c, "");
+#if __CHAR_BIT__ * __SIZEOF_LONG_LONG__ >= 16
+static_assert (__builtin_bitexpandg ((unsigned long long) 0xebd4U, (unsigned long long) 0x93bfU) == (unsigned long long) 0x1394, "");
+static_assert (__builtin_bitexpandg ((unsigned long long) 0xa166U, (unsigned long long) 0x001dU) == (unsigned long long) 0xc, "");
+#endif
+#if __CHAR_BIT__ * __SIZEOF_LONG_LONG__ >= 32
+static_assert (__builtin_bitexpandg ((unsigned long long) 0x9daeebd4UL, (unsigned long long) 0xad8793bfUL) == (unsigned long long) 0xa9851394UL, "");
+static_assert (__builtin_bitexpandg ((unsigned long long) 0x979ba166UL, (unsigned long long) 0x54ca001dUL) == (unsigned long long) 0x448000cUL, "");
+#endif
+#if __CHAR_BIT__ * __SIZEOF_LONG_LONG__ >= 64
+static_assert (__builtin_bitexpandg ((unsigned long long) 0x5bd30f069daeebd4ULL, (unsigned long long) 0x64f9a89bad8793bfULL) == (unsigned long long) 0x4a1a092a9851394ULL, "");
+static_assert (__builtin_bitexpandg ((unsigned long long) 0xb5672499979ba166ULL, (unsigned long long) 0xd315782354ca001dULL) == (unsigned long long) 0xd01470200448000cULL, "");
+#endif
+#ifdef __SIZEOF_INT128__
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0, (unsigned __int128) 0) == (unsigned __int128) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0x123456789abcdefULL, (unsigned __int128) 0) == (unsigned __int128) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0x123456789abcdefULL, (unsigned __int128) ~(unsigned __int128) 0) == (unsigned __int128) 0x123456789abcdefULL, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0, (unsigned __int128) 0x123456789abcdefULL) == (unsigned __int128) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) ~(unsigned __int128) 0, (unsigned __int128) ~(unsigned __int128) 0) == (unsigned __int128) ~(unsigned __int128) 0, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) ((((unsigned __int128) 1) << __builtin_popcountg ((unsigned __int128) 0x123456789abcdefULL)) - 1), (unsigned __int128) 0x123456789abcdefULL) == (unsigned __int128) 0x123456789abcdefULL, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0x4646464646464646ULL, (unsigned __int128) 0x5555555555555555ULL) == (unsigned __int128) 0x1014101410141014ULL, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0x1414141414141414ULL, (unsigned __int128) 0xaaaaaaaaaaaaaaaaULL) == (unsigned __int128) 0x0220022002200220ULL, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0xff, (unsigned __int128) 0xff) == 0xff, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0x0f, (unsigned __int128) 0xff) == 0x0f, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0x0f, (unsigned __int128) 0xf0) == 0xf0, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0xaaaaaaaaaaaaaaaaULL, (unsigned __int128) 0xccccccccccccccccULL) == (unsigned __int128) 0x8888888888888888ULL, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0x0f0f0f0f0f0f0f0fULL, (unsigned __int128) 0xaaaaaaaaaaaaaaaaULL) == (unsigned __int128) 0x00aa00aa00aa00aaULL, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0xd4, (unsigned __int128) 0xbf) == 0x94, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0x66, (unsigned __int128) 0x1d) == 0x0c, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0xebd4U, (unsigned __int128) 0x93bfU) == (unsigned __int128) 0x1394, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0xa166U, (unsigned __int128) 0x001dU) == (unsigned __int128) 0xc, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0x9daeebd4UL, (unsigned __int128) 0xad8793bfUL) == (unsigned __int128) 0xa9851394UL, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0x979ba166UL, (unsigned __int128) 0x54ca001dUL) == (unsigned __int128) 0x448000cUL, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0x5bd30f069daeebd4ULL, (unsigned __int128) 0x64f9a89bad8793bfULL) == (unsigned __int128) 0x4a1a092a9851394ULL, "");
+static_assert (__builtin_bitexpandg ((unsigned __int128) 0xb5672499979ba166ULL, (unsigned __int128) 0xd315782354ca001dULL) == (unsigned __int128) 0xd01470200448000cULL, "");
+#endif
+#if __BITINT_MAXWIDTH__ >= 256
+static_assert (__builtin_bitexpandg ((unsigned _BitInt(256)) 2977712650221868789329368963712957747535884589227943441826207435705422199671uwb,
+				     (unsigned _BitInt(256)) 71392704391611725846964173788815479408492634097466601526619781877369006838376uwb)
+	       == 6106334322603120698343087248751001114229967022894482914225634220926378496104uwb);
+static_assert (__builtin_bitexpandg ((unsigned _BitInt(256)) 71392704391611725846964173788815479408492634097466601526619781877369006838376uwb,
+				     (unsigned _BitInt(256)) 2977712650221868789329368963712957747535884589227943441826207435705422199671uwb)
+	       == 2715653213312698747932362475061475316365454769578495772994929980809745011024uwb);
+#endif
+#if __BITINT_MAXWIDTH__ >= 575
+static_assert (__builtin_bitexpandg ((unsigned _BitInt(575)) 118131541925402402155086157499115656285751034069848007471551618267237710886541403235442402972757841211601305512508765941610080588540585990221962870251502405139968332129617187uwb,
+				     (unsigned _BitInt(575)) 23115097167410578660280364356726488934202319035287675841474505237120386080336714513506555994051218240174733511200356850940493563860321836251689717522161899716121398557567847uwb)
+	       == 7623895243663960691569173075814666314012124130457458291983897390462352361781036204975171902420126018071090572104100014023386872050266171372969434719876916563037514178240771uwb);
+static_assert (__builtin_bitexpandg ((unsigned _BitInt(575)) 23115097167410578660280364356726488934202319035287675841474505237120386080336714513506555994051218240174733511200356850940493563860321836251689717522161899716121398557567847uwb,
+				     (unsigned _BitInt(575)) 118131541925402402155086157499115656285751034069848007471551618267237710886541403235442402972757841211601305512508765941610080588540585990221962870251502405139968332129617187uwb)
+	       == 56294931635403572486906377531414566093300447783103849622139379559580554463924172104226430624225347319693207529072644684462926168000686187753501958709265974532503455809323043uwb);
+#endif
+#if __BITINT_MAXWIDTH__ >= 1027
+static_assert (__builtin_bitexpandg ((unsigned _BitInt(1027)) 294473855873745241271551195901386950656729759273365562206638967800992654698032636902920911003782368139677793592762040049443703439321090202240002644548914408256861393744782128292349394935235968569736234857665376806506152875996067378466482077335451170878303723908011096841794981692284316910893612604487249472311uwb,
+				     (unsigned _BitInt(1027)) 540548418560750924575664703872106986534265345396756980894364393357877823859721129540519729973560861792682263121263569358086814437568179760344168293628388053637218170474254474264275367843920704704472260655775742000715646811343262012513538736719387133499156091882497205971330308173485560590613388499248986333824uwb)
+	       == 1053432602631601823738458435352232608180994474339675507824798318815508385108517845015392672754706461608810238033305170246166427250962973734897418531868110631156100837780702613467177557639965349351804007196695316749042516880235247409566358718996737210967041320563094935743638949079055891438475704252653642368uwb);
+static_assert (__builtin_bitexpandg ((unsigned _BitInt(1027)) 540548418560750924575664703872106986534265345396756980894364393357877823859721129540519729973560861792682263121263569358086814437568179760344168293628388053637218170474254474264275367843920704704472260655775742000715646811343262012513538736719387133499156091882497205971330308173485560590613388499248986333824uwb,
+				     (unsigned _BitInt(1027)) 294473855873745241271551195901386950656729759273365562206638967800992654698032636902920911003782368139677793592762040049443703439321090202240002644548914408256861393744782128292349394935235968569736234857665376806506152875996067378466482077335451170878303723908011096841794981692284316910893612604487249472311uwb)
+	       == 91355371957256612074255238122359885881282861027030297292884885291983886368774408697841433225727842530160483371399389894560044416587561512148347308387668958225153909170929379941956797733013538101336868524683698284851251557537245561688655263088925983144365058547397146385467295975230358276694698660737092454400uwb);
+#endif
+
+[[gnu::noipa]] unsigned char
+bitexpand8 (unsigned char x, unsigned char m)
+{
+  return __builtin_bitexpandg (x, m);
+}
+
+[[gnu::noipa]] unsigned short
+bitexpand16 (unsigned short x, unsigned short m)
+{
+  return __builtin_bitexpandg (x, m);
+}
+
+[[gnu::noipa]] unsigned int
+bitexpand32 (unsigned int x, unsigned int m)
+{
+  return __builtin_bitexpandg (x, m);
+}
+
+[[gnu::noipa]] unsigned long
+bitexpandl (unsigned long x, unsigned long m)
+{
+  return __builtin_bitexpandg (x, m);
+}
+
+[[gnu::noipa]] unsigned long long
+bitexpand64 (unsigned long long x, unsigned long long m)
+{
+  return __builtin_bitexpandg (x, m);
+}
+
+#ifdef __SIZEOF_INT128__
+[[gnu::noipa]] unsigned __int128
+bitexpand128 (unsigned __int128 x, unsigned __int128 m)
+{
+  return __builtin_bitexpandg (x, m);
+}
+#endif
+
+#if __BITINT_MAXWIDTH__ >= 256
+[[gnu::noipa]] unsigned _BitInt(256)
+bitexpand256 (unsigned _BitInt(256) x, unsigned _BitInt(256) y)
+{
+  return __builtin_bitexpandg (x, y);
+}
+#endif
+
+#if __BITINT_MAXWIDTH__ >= 575
+[[gnu::noipa]] unsigned _BitInt(575)
+bitexpand575 (unsigned _BitInt(575) x, unsigned _BitInt(575) y)
+{
+  return __builtin_bitexpandg (x, y);
+}
+#endif
+
+#if __BITINT_MAXWIDTH__ >= 1027
+[[gnu::noipa]] unsigned _BitInt(1027)
+bitexpand1027 (unsigned _BitInt(1027) x, unsigned _BitInt(1027) y)
+{
+  return __builtin_bitexpandg (x, y);
+}
+#endif
+
+#ifdef __SIZEOF_INT128__
+#define T unsigned __int128
+#else
+#define T unsigned long long
+#endif
+
+[[gnu::noipa]] T
+bitexpand8w (T x, T m)
+{
+  return bitexpand8 (x, m);
+}
+
+[[gnu::noipa]] T
+bitexpand16w (T x, T m)
+{
+  return bitexpand16 (x, m);
+}
+
+[[gnu::noipa]] T
+bitexpand32w (T x, T m)
+{
+  return bitexpand32 (x, m);
+}
+
+[[gnu::noipa]] T
+bitexpandlw (T x, T m)
+{
+  return bitexpandl (x, m);
+}
+
+[[gnu::noipa]] T
+bitexpand64w (T x, T m)
+{
+  return bitexpand64 (x, m);
+}
+
+int
+main ()
+{
+  struct { T (*fn) (T, T); unsigned int digits; } tests[] = {
+    { bitexpand8w, __CHAR_BIT__ },
+    { bitexpand16w, __CHAR_BIT__ * __SIZEOF_SHORT__ },
+    { bitexpand32w, __CHAR_BIT__ * __SIZEOF_INT__ },
+    { bitexpandlw, __CHAR_BIT__ * __SIZEOF_LONG__ },
+    { bitexpand64w, __CHAR_BIT__ * __SIZEOF_LONG_LONG__ },
+#ifdef __SIZEOF_INT128__
+    { bitexpand128, __CHAR_BIT__ * __SIZEOF_INT128__ },
+#endif
+  };
+  for (int i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
+    {
+      T mask = ~(T) 0;
+      if (tests[i].digits < sizeof (mask) * __CHAR_BIT__)
+	mask >>= sizeof (mask) * __CHAR_BIT__ - tests[i].digits;
+      if (tests[i].fn (0, 0) != 0)
+	__builtin_abort ();
+      if (tests[i].fn (0x123456789abcdefULL, 0) != 0)
+	__builtin_abort ();
+      if (tests[i].fn (0x123456789abcdefULL, ~0ULL) != (0x123456789abcdefULL & mask))
+	__builtin_abort ();
+      if (tests[i].fn (0, 0x123456789abcdefULL) != 0)
+	__builtin_abort ();
+      if (tests[i].fn (~0ULL, ~0ULL) != (~0ULL & mask))
+	__builtin_abort ();
+      if (tests[i].fn ((1ULL << __builtin_popcountg (0x123456789abcdefULL & mask)) - 1,
+		       0x123456789abcdefULL) != (0x123456789abcdefULL & mask))
+	__builtin_abort ();
+      if (tests[i].digits >= 16)
+	{
+	  if (tests[i].fn (0x4646464646464646ULL, 0x5555555555555555ULL)
+	      != (0x1014101410141014ULL & mask))
+	    __builtin_abort ();
+	  if (tests[i].fn (0x1414141414141414ULL, 0xaaaaaaaaaaaaaaaaULL)
+	      != (0x0220022002200220ULL & mask))
+	    __builtin_abort ();
+	}
+      if (tests[i].fn (0xff, 0xff) != 0xff)
+	__builtin_abort ();
+      if (tests[i].fn (0x0f, 0xff) != 0x0f)
+	__builtin_abort ();
+      if (tests[i].fn (0x0f, 0xf0) != 0xf0)
+	__builtin_abort ();
+      if (tests[i].fn (0xaaaaaaaaaaaaaaaaULL, 0xccccccccccccccccULL)
+	  != (0x8888888888888888ULL & mask))
+	__builtin_abort ();
+      if (tests[i].fn (0x0f0f0f0f0f0f0f0fULL, 0xaaaaaaaaaaaaaaaaULL)
+	  != (0x00aa00aa00aa00aaULL & mask))
+	__builtin_abort ();
+      if (tests[i].fn (0xd4, 0xbf) != 0x94)
+	__builtin_abort ();
+      if (tests[i].fn (0x66, 0x1d) != 0x0c)
+	__builtin_abort ();
+      if (tests[i].digits < 16)
+	continue;
+      if (tests[i].fn (0xebd4U, 0x93bfU) != 0x1394)
+	__builtin_abort ();
+      if (tests[i].fn (0xa166U, 0x001dU) != 0xc)
+	__builtin_abort ();
+      if (tests[i].digits < 32)
+	continue;
+      if (tests[i].fn (0x9daeebd4UL, 0xad8793bfUL) != 0xa9851394UL)
+	__builtin_abort ();
+      if (tests[i].fn (0x979ba166UL, 0x54ca001dUL) != 0x448000cUL)
+	__builtin_abort ();
+      if (tests[i].digits < 64)
+	continue;
+      if (tests[i].fn (0x5bd30f069daeebd4ULL, 0x64f9a89bad8793bfULL) != 0x4a1a092a9851394ULL)
+	__builtin_abort ();
+      if (tests[i].fn (0xb5672499979ba166ULL, 0xd315782354ca001dULL) != 0xd01470200448000cULL)
+	__builtin_abort ();
+    }
+#if __BITINT_MAXWIDTH__ >= 256
+  {
+    const unsigned _BitInt(256) a = 2977712650221868789329368963712957747535884589227943441826207435705422199671uwb;
+    const unsigned _BitInt(256) b = 71392704391611725846964173788815479408492634097466601526619781877369006838376uwb;
+    if (bitexpand256 (a, b) != __builtin_bitexpandg (a, b)
+	|| bitexpand256 (-1, -1) != -1
+	|| bitexpand256 (0, 0) != 0
+	|| bitexpand256 (b, a) != __builtin_bitexpandg (b, a))
+      __builtin_abort ();
+  }
+#endif
+#if __BITINT_MAXWIDTH__ >= 575
+  {
+    const unsigned _BitInt(575) a = 118131541925402402155086157499115656285751034069848007471551618267237710886541403235442402972757841211601305512508765941610080588540585990221962870251502405139968332129617187uwb;
+    const unsigned _BitInt(575) b = 23115097167410578660280364356726488934202319035287675841474505237120386080336714513506555994051218240174733511200356850940493563860321836251689717522161899716121398557567847uwb;
+    if (bitexpand575 (a, b) != __builtin_bitexpandg (a, b)
+	|| bitexpand575 (-1, -1) != -1
+	|| bitexpand575 (0, 0) != 0
+	|| bitexpand575 (b, a) != __builtin_bitexpandg (b, a))
+      __builtin_abort ();
+  }
+#endif
+#if __BITINT_MAXWIDTH__ >= 1027
+  {
+    const unsigned _BitInt(1027) a = 294473855873745241271551195901386950656729759273365562206638967800992654698032636902920911003782368139677793592762040049443703439321090202240002644548914408256861393744782128292349394935235968569736234857665376806506152875996067378466482077335451170878303723908011096841794981692284316910893612604487249472311uwb;
+    const unsigned _BitInt(1027) b = 540548418560750924575664703872106986534265345396756980894364393357877823859721129540519729973560861792682263121263569358086814437568179760344168293628388053637218170474254474264275367843920704704472260655775742000715646811343262012513538736719387133499156091882497205971330308173485560590613388499248986333824uwb;
+    if (bitexpand1027 (a, b) != __builtin_bitexpandg (a, b)
+	|| bitexpand1027 (-1, -1) != -1
+	|| bitexpand1027 (0, 0) != 0
+	|| bitexpand1027 (b, a) != __builtin_bitexpandg (b, a))
+      __builtin_abort ();
+  }
+#endif
+}
--- a/gcc/testsuite/c-c++-common/builtin-bitcompressg-2.c	2026-11-04 17:23:29.432521972 +0100
+++ b/gcc/testsuite/c-c++-common/builtin-bitcompressg-2.c	2026-11-04 17:23:29.432521972 +0100
@@ -0,0 +1,36 @@
+/* Test __builtin_bitcompressg.  */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+int
+main ()
+{
+  unsigned int a;
+  __builtin_bitcompressg ();			/* { dg-error "wrong number of arguments to '__builtin_bitcompressg'" } */
+  __builtin_bitcompressg (1U);			/* { dg-error "wrong number of arguments to '__builtin_bitcompressg'" } */
+  __builtin_bitcompressg (1U, 1U, &a);		/* { dg-error "wrong number of arguments to '__builtin_bitcompressg'" } */
+  __builtin_bitcompressg (1U, 1U, &a, 1U);	/* { dg-error "wrong number of arguments to '__builtin_bitcompressg'" } */
+  __builtin_bitcompressg (1.0, 1.0);		/* { dg-error "'__builtin_bitcompressg' operand not an integral type" } */
+  __builtin_bitcompressg (1.0f, 1.0f);		/* { dg-error "'__builtin_bitcompressg' operand not an integral type" } */
+  __builtin_bitcompressg (1.0L, 1.0L);		/* { dg-error "'__builtin_bitcompressg' operand not an integral type" } */
+  struct S { int a; };
+  __builtin_bitcompressg ((struct S) { 42 }, (struct S) { 43 }); /* { dg-error "'__builtin_bitcompressg' operand not an integral type" } */
+  enum E { E0, E1 } e;
+  __builtin_bitcompressg (E1, E1);		/* { dg-error "argument 1 in call to function '__builtin_bitcompressg' has signed type" "" { target c } } */
+						/* { dg-error "argument 1 in call to function '__builtin_bitcompressg' has enumerated type" "" { target c++ } .-1 } */
+  __builtin_bitcompressg (e, e);		/* { dg-error "argument 1 in call to function '__builtin_bitcompressg' has enumerated type" } */
+  __builtin_bitcompressg ((enum E) 1, (enum E) 1); /* { dg-error "argument 1 in call to function '__builtin_bitcompressg' has enumerated type" } */
+#if !defined(__cplusplus) || __cplusplus >=  201103L
+  enum F : int { F0, F1 } f;
+  __builtin_bitcompressg (F1, F1);		/* { dg-error "argument 1 in call to function '__builtin_bitcompressg' has enumerated type" "" { target { c || c++11 } } } */
+  __builtin_bitcompressg (f, f);		/* { dg-error "argument 1 in call to function '__builtin_bitcompressg' has enumerated type" "" { target { c || c++11 } } } */
+  __builtin_bitcompressg ((enum F) 1, (enum F) 1); /* { dg-error "argument 1 in call to function '__builtin_bitcompressg' has enumerated type" "" { target { c || c++11 } } } */
+#endif
+  __builtin_bitcompressg (false, true);		/* { dg-error "argument 1 in call to function '__builtin_bitcompressg' has boolean type" } */
+  __builtin_bitcompressg (1, 1);		/* { dg-error "argument 1 in call to function '__builtin_bitcompressg' has signed type" } */
+  __builtin_bitcompressg ((char) 1, (char) 1);	/* { dg-error "argument 1 in call to function '__builtin_bitcompressg' has signed type" "" { target { ! unsigned_char } } } */
+  __builtin_bitcompressg (1UL, 1ULL);		/* { dg-error "arguments 1 and 2 to function '__builtin_bitcompressg' have different types 'long unsigned int' and 'long long unsigned int'" } */
+  __builtin_bitcompressg (1U, 1.0);		/* { dg-error "arguments 1 and 2 to function '__builtin_bitcompressg' have different types 'unsigned int' and 'double'" } */
+  __builtin_bitcompressg ((unsigned char) 1, (char) 1); /* { dg-error "arguments 1 and 2 to function '__builtin_bitcompressg' have different types 'unsigned char' and 'char'" } */
+  __builtin_bitcompressg ((unsigned char) 1, true); /* { dg-error "arguments 1 and 2 to function '__builtin_bitcompressg' have different types 'unsigned char' and 'bool'" } */
+}
--- a/gcc/testsuite/c-c++-common/builtin-bitcompressg-1.c	2026-11-04 17:23:29.432271221 +0100
+++ b/gcc/testsuite/c-c++-common/builtin-bitcompressg-1.c	2026-11-05 12:16:15.623985065 +0100
@@ -0,0 +1,364 @@
+/* Test __builtin_bitcompressg.  */
+/* { dg-do run { target { c || c++11 } } } */
+/* { dg-options "-O2" } */
+
+#if !__has_builtin(__builtin_bitcompressg)
+#error __builtin_bitcompressg unsupported
+#endif
+
+static_assert (__builtin_bitcompressg ((unsigned char) 0, (unsigned char) 0) == (unsigned char) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned char) 0x123456789abcdefULL, (unsigned char) 0) == (unsigned char) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned char) 0x123456789abcdefULL, (unsigned char) ~(unsigned char) 0) == (unsigned char) 0x123456789abcdefULL, "");
+static_assert (__builtin_bitcompressg ((unsigned char) 0, (unsigned char) 0x123456789abcdefULL) == (unsigned char) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned char) ~(unsigned char) 0, (unsigned char) ~(unsigned char) 0) == (unsigned char) ~(unsigned char) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned char) ~(unsigned char) 0, (unsigned char) 0x123456789abcdefULL) == (((unsigned char) 1) << __builtin_popcountg ((unsigned char) 0x123456789abcdefULL)) - 1, "");
+static_assert (__builtin_bitcompressg ((unsigned char) 0xff, (unsigned char) 0xff) == (unsigned char) 0xff, "");
+static_assert (__builtin_bitcompressg ((unsigned char) 0xff, (unsigned char) 0x0f) == (unsigned char) 0x0f, "");
+static_assert (__builtin_bitcompressg ((unsigned char) 0xff, (unsigned char) 0xf0) == (unsigned char) 0x0f, "");
+#if (__CHAR_BIT__ % 2) == 0
+static_assert (__builtin_bitcompressg ((unsigned char) 0xaaaaaaaaaaaaaaaaULL, (unsigned char) 0xccccccccccccccccULL) == ((unsigned char) 0xaaaaaaaaaaaaaaaaULL) >> (__CHAR_BIT__ / 2), "");
+static_assert (__builtin_bitcompressg ((unsigned char) ~(unsigned char) 0, (unsigned char) 0xaaaaaaaaaaaaaaaaULL) == ((unsigned char) ~(unsigned char) 0) >> (__CHAR_BIT__ / 2), "");
+#endif
+static_assert (__builtin_bitcompressg ((unsigned char) 0xd4, (unsigned char) 0xbf) == (unsigned char) 0x54, "");
+static_assert (__builtin_bitcompressg ((unsigned char) 0x66, (unsigned char) 0x1d) == (unsigned char) 0x02, "");
+static_assert (__builtin_bitcompressg ((unsigned short) 0, (unsigned short) 0) == (unsigned short) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned short) 0x123456789abcdefULL, (unsigned short) 0) == (unsigned short) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned short) 0x123456789abcdefULL, (unsigned short) ~(unsigned short) 0) == (unsigned short) 0x123456789abcdefULL, "");
+static_assert (__builtin_bitcompressg ((unsigned short) 0, (unsigned short) 0x123456789abcdefULL) == (unsigned short) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned short) ~(unsigned short) 0, (unsigned short) ~(unsigned short) 0) == (unsigned short) ~(unsigned short) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned short) ~(unsigned short) 0, (unsigned short) 0x123456789abcdefULL) == (((unsigned short) 1) << __builtin_popcountg ((unsigned short) 0x123456789abcdefULL)) - 1, "");
+#if __CHAR_BIT__ * __SIZEOF_SHORT__ >= 16 && (__CHAR_BIT__ * __SIZEOF_SHORT__ % 2) == 0
+static_assert (__builtin_bitcompressg ((unsigned short) 0x1234123412341234ULL, (unsigned short) 0x5555555555555555ULL) == ((unsigned short) 0x4646464646464646ULL) >> (__CHAR_BIT__ * __SIZEOF_SHORT__ / 2), "");
+static_assert (__builtin_bitcompressg ((unsigned short) 0x1234123412341234ULL, (unsigned short) 0xaaaaaaaaaaaaaaaaULL) == ((unsigned short) 0x1414141414141414ULL) >> (__CHAR_BIT__ * __SIZEOF_SHORT__ / 2), "");
+#endif
+static_assert (__builtin_bitcompressg ((unsigned short) 0xff, (unsigned short) 0xff) == (unsigned short) 0xff, "");
+static_assert (__builtin_bitcompressg ((unsigned short) 0xff, (unsigned short) 0x0f) == (unsigned short) 0x0f, "");
+static_assert (__builtin_bitcompressg ((unsigned short) 0xff, (unsigned short) 0xf0) == (unsigned short) 0x0f, "");
+#if (__CHAR_BIT__ * __SIZEOF_SHORT__ % 2) == 0
+static_assert (__builtin_bitcompressg ((unsigned short) 0xaaaaaaaaaaaaaaaaULL, (unsigned short) 0xccccccccccccccccULL) == ((unsigned short) 0xaaaaaaaaaaaaaaaaULL) >> (__CHAR_BIT__ * __SIZEOF_SHORT__ / 2), "");
+static_assert (__builtin_bitcompressg ((unsigned short) ~(unsigned short) 0, (unsigned short) 0xaaaaaaaaaaaaaaaaULL) == ((unsigned short) ~(unsigned short) 0) >> (__CHAR_BIT__ * __SIZEOF_SHORT__ / 2), "");
+#endif
+static_assert (__builtin_bitcompressg ((unsigned short) 0xd4, (unsigned short) 0xbf) == (unsigned short) 0x54, "");
+static_assert (__builtin_bitcompressg ((unsigned short) 0x66, (unsigned short) 0x1d) == (unsigned short) 0x02, "");
+#if __CHAR_BIT__ * __SIZEOF_SHORT__ >= 16
+static_assert (__builtin_bitcompressg ((unsigned short) 0xebd4U, (unsigned short) 0x93bfU) == (unsigned short) 0x05d4, "");
+static_assert (__builtin_bitcompressg ((unsigned short) 0xa166U, (unsigned short) 0x001dU) == (unsigned short) 2, "");
+#endif
+static_assert (__builtin_bitcompressg ((unsigned int) 0, (unsigned int) 0) == (unsigned int) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned int) 0x123456789abcdefULL, (unsigned int) 0) == (unsigned int) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned int) 0x123456789abcdefULL, (unsigned int) ~(unsigned int) 0) == (unsigned int) 0x123456789abcdefULL, "");
+static_assert (__builtin_bitcompressg ((unsigned int) 0, (unsigned int) 0x123456789abcdefULL) == (unsigned int) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned int) ~(unsigned int) 0, (unsigned int) ~(unsigned int) 0) == (unsigned int) ~(unsigned int) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned int) ~(unsigned int) 0, (unsigned int) 0x123456789abcdefULL) == (((unsigned int) 1) << __builtin_popcountg ((unsigned int) 0x123456789abcdefULL)) - 1, "");
+#if __CHAR_BIT__ * __SIZEOF_INT__ >= 16 && (__CHAR_BIT__ * __SIZEOF_INT__ % 2) == 0
+static_assert (__builtin_bitcompressg ((unsigned int) 0x1234123412341234ULL, (unsigned int) 0x5555555555555555ULL) == ((unsigned int) 0x4646464646464646ULL) >> (__CHAR_BIT__ * __SIZEOF_INT__ / 2), "");
+static_assert (__builtin_bitcompressg ((unsigned int) 0x1234123412341234ULL, (unsigned int) 0xaaaaaaaaaaaaaaaaULL) == ((unsigned int) 0x1414141414141414ULL) >> (__CHAR_BIT__ * __SIZEOF_INT__ / 2), "");
+#endif
+static_assert (__builtin_bitcompressg ((unsigned int) 0xff, (unsigned int) 0xff) == (unsigned int) 0xff, "");
+static_assert (__builtin_bitcompressg ((unsigned int) 0xff, (unsigned int) 0x0f) == (unsigned int) 0x0f, "");
+static_assert (__builtin_bitcompressg ((unsigned int) 0xff, (unsigned int) 0xf0) == (unsigned int) 0x0f, "");
+#if (__CHAR_BIT__ * __SIZEOF_INT__ % 2) == 0
+static_assert (__builtin_bitcompressg ((unsigned int) 0xaaaaaaaaaaaaaaaaULL, (unsigned int) 0xccccccccccccccccULL) == ((unsigned int) 0xaaaaaaaaaaaaaaaaULL) >> (__CHAR_BIT__ * __SIZEOF_INT__ / 2), "");
+static_assert (__builtin_bitcompressg ((unsigned int) ~(unsigned int) 0, (unsigned int) 0xaaaaaaaaaaaaaaaaULL) == ((unsigned int) ~(unsigned int) 0) >> (__CHAR_BIT__ * __SIZEOF_INT__ / 2), "");
+#endif
+static_assert (__builtin_bitcompressg ((unsigned int) 0xd4, (unsigned int) 0xbf) == (unsigned int) 0x54, "");
+static_assert (__builtin_bitcompressg ((unsigned int) 0x66, (unsigned int) 0x1d) == (unsigned int) 0x02, "");
+#if __CHAR_BIT__ * __SIZEOF_INT__ >= 16
+static_assert (__builtin_bitcompressg ((unsigned int) 0xebd4U, (unsigned int) 0x93bfU) == (unsigned int) 0x05d4, "");
+static_assert (__builtin_bitcompressg ((unsigned int) 0xa166U, (unsigned int) 0x001dU) == (unsigned int) 2, "");
+#endif
+#if __CHAR_BIT__ * __SIZEOF_INT__ >= 32
+static_assert (__builtin_bitcompressg ((unsigned int) 0x9daeebd4U, (unsigned int) 0xad8793bfU) == (unsigned int) 0xbf5d4U, "");
+static_assert (__builtin_bitcompressg ((unsigned int) 0x979ba166U, (unsigned int) 0x54ca001dU) == (unsigned int) 0x3b2U, "");
+#endif
+static_assert (__builtin_bitcompressg ((unsigned long) 0, (unsigned long) 0) == (unsigned long) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned long) 0x123456789abcdefULL, (unsigned long) 0) == (unsigned long) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned long) 0x123456789abcdefULL, (unsigned long) ~(unsigned long) 0) == (unsigned long) 0x123456789abcdefULL, "");
+static_assert (__builtin_bitcompressg ((unsigned long) 0, (unsigned long) 0x123456789abcdefULL) == (unsigned long) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned long) ~(unsigned long) 0, (unsigned long) ~(unsigned long) 0) == (unsigned long) ~(unsigned long) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned long) ~(unsigned long) 0, (unsigned long) 0x123456789abcdefULL) == (((unsigned long) 1) << __builtin_popcountg ((unsigned long) 0x123456789abcdefULL)) - 1, "");
+#if __CHAR_BIT__ * __SIZEOF_LONG__ >= 16 && (__CHAR_BIT__ * __SIZEOF_LONG__ % 2) == 0
+static_assert (__builtin_bitcompressg ((unsigned long) 0x1234123412341234ULL, (unsigned long) 0x5555555555555555ULL) == ((unsigned long) 0x4646464646464646ULL) >> (__CHAR_BIT__ * __SIZEOF_LONG__ / 2), "");
+static_assert (__builtin_bitcompressg ((unsigned long) 0x1234123412341234ULL, (unsigned long) 0xaaaaaaaaaaaaaaaaULL) == ((unsigned long) 0x1414141414141414ULL) >> (__CHAR_BIT__ * __SIZEOF_LONG__ / 2), "");
+#endif
+static_assert (__builtin_bitcompressg ((unsigned long) 0xff, (unsigned long) 0xff) == (unsigned long) 0xff, "");
+static_assert (__builtin_bitcompressg ((unsigned long) 0xff, (unsigned long) 0x0f) == (unsigned long) 0x0f, "");
+static_assert (__builtin_bitcompressg ((unsigned long) 0xff, (unsigned long) 0xf0) == (unsigned long) 0x0f, "");
+#if (__CHAR_BIT__ * __SIZEOF_LONG__ % 2) == 0
+static_assert (__builtin_bitcompressg ((unsigned long) 0xaaaaaaaaaaaaaaaaULL, (unsigned long) 0xccccccccccccccccULL) == ((unsigned long) 0xaaaaaaaaaaaaaaaaULL) >> (__CHAR_BIT__ * __SIZEOF_LONG__ / 2), "");
+static_assert (__builtin_bitcompressg ((unsigned long) ~(unsigned long) 0, (unsigned long) 0xaaaaaaaaaaaaaaaaULL) == ((unsigned long) ~(unsigned long) 0) >> (__CHAR_BIT__ * __SIZEOF_LONG__ / 2), "");
+#endif
+static_assert (__builtin_bitcompressg ((unsigned long) 0xd4, (unsigned long) 0xbf) == (unsigned long) 0x54, "");
+static_assert (__builtin_bitcompressg ((unsigned long) 0x66, (unsigned long) 0x1d) == (unsigned long) 0x02, "");
+#if __CHAR_BIT__ * __SIZEOF_LONG__ >= 16
+static_assert (__builtin_bitcompressg ((unsigned long) 0xebd4U, (unsigned long) 0x93bfU) == (unsigned long) 0x05d4, "");
+static_assert (__builtin_bitcompressg ((unsigned long) 0xa166U, (unsigned long) 0x001dU) == (unsigned long) 2, "");
+#endif
+#if __CHAR_BIT__ * __SIZEOF_LONG__ >= 32
+static_assert (__builtin_bitcompressg ((unsigned long) 0x9daeebd4U, (unsigned long) 0xad8793bfU) == (unsigned long) 0xbf5d4U, "");
+static_assert (__builtin_bitcompressg ((unsigned long) 0x979ba166U, (unsigned long) 0x54ca001dU) == (unsigned long) 0x3b2U, "");
+#endif
+#if __CHAR_BIT__ * __SIZEOF_LONG__ >= 64
+static_assert (__builtin_bitcompressg ((unsigned long) 0x5bd30f069daeebd4ULL, (unsigned long) 0x64f9a89bad8793bfULL) == (unsigned long) 0x13522bf5d4ULL, "");
+static_assert (__builtin_bitcompressg ((unsigned long) 0xb5672499979ba166ULL, (unsigned long) 0xd315782354ca001dULL) == (unsigned long) 0x2ad0bb2ULL, "");
+#endif
+static_assert (__builtin_bitcompressg ((unsigned long long) 0, (unsigned long long) 0) == (unsigned long long) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned long long) 0x123456789abcdefULL, (unsigned long long) 0) == (unsigned long long) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned long long) 0x123456789abcdefULL, (unsigned long long) ~(unsigned long long) 0) == (unsigned long long) 0x123456789abcdefULL, "");
+static_assert (__builtin_bitcompressg ((unsigned long long) 0, (unsigned long long) 0x123456789abcdefULL) == (unsigned long long) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned long long) ~(unsigned long long) 0, (unsigned long long) ~(unsigned long long) 0) == (unsigned long long) ~(unsigned long long) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned long long) ~(unsigned long long) 0, (unsigned long long) 0x123456789abcdefULL) == (((unsigned long long) 1) << __builtin_popcountg ((unsigned long long) 0x123456789abcdefULL)) - 1, "");
+#if __CHAR_BIT__ * __SIZEOF_LONG_LONG__ >= 16 && (__CHAR_BIT__ * __SIZEOF_LONG_LONG__ % 2) == 0
+static_assert (__builtin_bitcompressg ((unsigned long long) 0x1234123412341234ULL, (unsigned long long) 0x5555555555555555ULL) == ((unsigned long long) 0x4646464646464646ULL) >> (__CHAR_BIT__ * __SIZEOF_LONG_LONG__ / 2), "");
+static_assert (__builtin_bitcompressg ((unsigned long long) 0x1234123412341234ULL, (unsigned long long) 0xaaaaaaaaaaaaaaaaULL) == ((unsigned long long) 0x1414141414141414ULL) >> (__CHAR_BIT__ * __SIZEOF_LONG_LONG__ / 2), "");
+#endif
+static_assert (__builtin_bitcompressg ((unsigned long long) 0xff, (unsigned long long) 0xff) == (unsigned long long) 0xff, "");
+static_assert (__builtin_bitcompressg ((unsigned long long) 0xff, (unsigned long long) 0x0f) == (unsigned long long) 0x0f, "");
+static_assert (__builtin_bitcompressg ((unsigned long long) 0xff, (unsigned long long) 0xf0) == (unsigned long long) 0x0f, "");
+#if (__CHAR_BIT__ * __SIZEOF_LONG_LONG__ % 2) == 0
+static_assert (__builtin_bitcompressg ((unsigned long long) 0xaaaaaaaaaaaaaaaaULL, (unsigned long long) 0xccccccccccccccccULL) == ((unsigned long long) 0xaaaaaaaaaaaaaaaaULL) >> (__CHAR_BIT__ * __SIZEOF_LONG_LONG__ / 2), "");
+static_assert (__builtin_bitcompressg ((unsigned long long) ~(unsigned long long) 0, (unsigned long long) 0xaaaaaaaaaaaaaaaaULL) == ((unsigned long long) ~(unsigned long long) 0) >> (__CHAR_BIT__ * __SIZEOF_LONG_LONG__ / 2), "");
+#endif
+static_assert (__builtin_bitcompressg ((unsigned long long) 0xd4, (unsigned long long) 0xbf) == (unsigned long long) 0x54, "");
+static_assert (__builtin_bitcompressg ((unsigned long long) 0x66, (unsigned long long) 0x1d) == (unsigned long long) 0x02, "");
+#if __CHAR_BIT__ * __SIZEOF_LONG_LONG__ >= 16
+static_assert (__builtin_bitcompressg ((unsigned long long) 0xebd4U, (unsigned long long) 0x93bfU) == (unsigned long long) 0x05d4, "");
+static_assert (__builtin_bitcompressg ((unsigned long long) 0xa166U, (unsigned long long) 0x001dU) == (unsigned long long) 2, "");
+#endif
+#if __CHAR_BIT__ * __SIZEOF_LONG_LONG__ >= 32
+static_assert (__builtin_bitcompressg ((unsigned long long) 0x9daeebd4U, (unsigned long long) 0xad8793bfU) == (unsigned long long) 0xbf5d4U, "");
+static_assert (__builtin_bitcompressg ((unsigned long long) 0x979ba166U, (unsigned long long) 0x54ca001dU) == (unsigned long long) 0x3b2U, "");
+#endif
+#if __CHAR_BIT__ * __SIZEOF_LONG_LONG__ >= 64
+static_assert (__builtin_bitcompressg ((unsigned long long) 0x5bd30f069daeebd4ULL, (unsigned long long) 0x64f9a89bad8793bfULL) == (unsigned long long) 0x13522bf5d4ULL, "");
+static_assert (__builtin_bitcompressg ((unsigned long long) 0xb5672499979ba166ULL, (unsigned long long) 0xd315782354ca001dULL) == (unsigned long long) 0x2ad0bb2ULL, "");
+#endif
+#ifdef __SIZEOF_INT128__
+static_assert (__builtin_bitcompressg ((unsigned __int128) 0, (unsigned __int128) 0) == (unsigned __int128) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) 0x123456789abcdefULL, (unsigned __int128) 0) == (unsigned __int128) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) 0x123456789abcdefULL, (unsigned __int128) ~(unsigned __int128) 0) == (unsigned __int128) 0x123456789abcdefULL, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) 0, (unsigned __int128) 0x123456789abcdefULL) == (unsigned __int128) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) ~(unsigned __int128) 0, (unsigned __int128) ~(unsigned __int128) 0) == (unsigned __int128) ~(unsigned __int128) 0, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) ~(unsigned __int128) 0, (unsigned __int128) 0x123456789abcdefULL) == (((unsigned __int128) 1) << __builtin_popcountg ((unsigned __int128) 0x123456789abcdefULL)) - 1, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) 0xff, (unsigned __int128) 0xff) == (unsigned __int128) 0xff, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) 0xff, (unsigned __int128) 0x0f) == (unsigned __int128) 0x0f, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) 0xff, (unsigned __int128) 0xf0) == (unsigned __int128) 0x0f, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) 0xd4, (unsigned __int128) 0xbf) == (unsigned __int128) 0x54, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) 0x66, (unsigned __int128) 0x1d) == (unsigned __int128) 0x02, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) 0xebd4U, (unsigned __int128) 0x93bfU) == (unsigned __int128) 0x05d4, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) 0xa166U, (unsigned __int128) 0x001dU) == (unsigned __int128) 2, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) 0x9daeebd4U, (unsigned __int128) 0xad8793bfU) == (unsigned __int128) 0xbf5d4U, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) 0x979ba166U, (unsigned __int128) 0x54ca001dU) == (unsigned __int128) 0x3b2U, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) 0x5bd30f069daeebd4ULL, (unsigned __int128) 0x64f9a89bad8793bfULL) == (unsigned __int128) 0x13522bf5d4ULL, "");
+static_assert (__builtin_bitcompressg ((unsigned __int128) 0xb5672499979ba166ULL, (unsigned __int128) 0xd315782354ca001dULL) == (unsigned __int128) 0x2ad0bb2ULL, "");
+#endif
+#if __BITINT_MAXWIDTH__ >= 256
+static_assert (__builtin_bitcompressg ((unsigned _BitInt(256)) 2977712650221868789329368963712957747535884589227943441826207435705422199671uwb,
+				       (unsigned _BitInt(256)) 71392704391611725846964173788815479408492634097466601526619781877369006838376uwb)
+	       == 7351178182929446285521030097634422236446uwb);
+static_assert (__builtin_bitcompressg ((unsigned _BitInt(256)) 71392704391611725846964173788815479408492634097466601526619781877369006838376uwb,
+				       (unsigned _BitInt(256)) 2977712650221868789329368963712957747535884589227943441826207435705422199671uwb)
+	       == 7792001651588212291781843281756166832uwb);
+#endif
+#if __BITINT_MAXWIDTH__ >= 575
+static_assert (__builtin_bitcompressg ((unsigned _BitInt(575)) 118131541925402402155086157499115656285751034069848007471551618267237710886541403235442402972757841211601305512508765941610080588540585990221962870251502405139968332129617187uwb,
+				       (unsigned _BitInt(575)) 23115097167410578660280364356726488934202319035287675841474505237120386080336714513506555994051218240174733511200356850940493563860321836251689717522161899716121398557567847uwb)
+	       == 327929344718748401627780651081744473244583534757535979608430300553738652522639530891741739uwb);
+static_assert (__builtin_bitcompressg ((unsigned _BitInt(575)) 23115097167410578660280364356726488934202319035287675841474505237120386080336714513506555994051218240174733511200356850940493563860321836251689717522161899716121398557567847uwb,
+				       (unsigned _BitInt(575)) 118131541925402402155086157499115656285751034069848007471551618267237710886541403235442402972757841211601305512508765941610080588540585990221962870251502405139968332129617187uwb)
+	       == 2909031437005614947964838829905837773096498383064307256216302161781782214762584910989359uwb);
+#endif
+#if __BITINT_MAXWIDTH__ >= 1027
+static_assert (__builtin_bitcompressg ((unsigned _BitInt(1027)) 294473855873745241271551195901386950656729759273365562206638967800992654698032636902920911003782368139677793592762040049443703439321090202240002644548914408256861393744782128292349394935235968569736234857665376806506152875996067378466482077335451170878303723908011096841794981692284316910893612604487249472311uwb,
+				       (unsigned _BitInt(1027)) 540548418560750924575664703872106986534265345396756980894364393357877823859721129540519729973560861792682263121263569358086814437568179760344168293628388053637218170474254474264275367843920704704472260655775742000715646811343262012513538736719387133499156091882497205971330308173485560590613388499248986333824uwb)
+	       == 1345657303990754153143578283893576247665548280540247072591967983899466500545664996992558492135467363277229823156703874541619044608181549372125254374uwb);
+static_assert (__builtin_bitcompressg ((unsigned _BitInt(1027)) 540548418560750924575664703872106986534265345396756980894364393357877823859721129540519729973560861792682263121263569358086814437568179760344168293628388053637218170474254474264275367843920704704472260655775742000715646811343262012513538736719387133499156091882497205971330308173485560590613388499248986333824uwb,
+				       (unsigned _BitInt(1027)) 294473855873745241271551195901386950656729759273365562206638967800992654698032636902920911003782368139677793592762040049443703439321090202240002644548914408256861393744782128292349394935235968569736234857665376806506152875996067378466482077335451170878303723908011096841794981692284316910893612604487249472311uwb)
+	       == 450453790383945335553643124189396588460957680877946449247954125046510191930910925667177435230534006187607866941914049655956462502374002973515990229312uwb);
+#endif
+
+[[gnu::noipa]] unsigned char
+bitcompress8 (unsigned char x, unsigned char m)
+{
+  return __builtin_bitcompressg (x, m);
+}
+
+[[gnu::noipa]] unsigned short
+bitcompress16 (unsigned short x, unsigned short m)
+{
+  return __builtin_bitcompressg (x, m);
+}
+
+[[gnu::noipa]] unsigned int
+bitcompress32 (unsigned int x, unsigned int m)
+{
+  return __builtin_bitcompressg (x, m);
+}
+
+[[gnu::noipa]] unsigned long
+bitcompressl (unsigned long x, unsigned long m)
+{
+  return __builtin_bitcompressg (x, m);
+}
+
+[[gnu::noipa]] unsigned long long
+bitcompress64 (unsigned long long x, unsigned long long m)
+{
+  return __builtin_bitcompressg (x, m);
+}
+
+#ifdef __SIZEOF_INT128__
+[[gnu::noipa]] unsigned __int128
+bitcompress128 (unsigned __int128 x, unsigned __int128 m)
+{
+  return __builtin_bitcompressg (x, m);
+}
+#endif
+
+#if __BITINT_MAXWIDTH__ >= 256
+[[gnu::noipa]] unsigned _BitInt(256)
+bitcompress256 (unsigned _BitInt(256) x, unsigned _BitInt(256) y)
+{
+  return __builtin_bitcompressg (x, y);
+}
+#endif
+
+#if __BITINT_MAXWIDTH__ >= 575
+[[gnu::noipa]] unsigned _BitInt(575)
+bitcompress575 (unsigned _BitInt(575) x, unsigned _BitInt(575) y)
+{
+  return __builtin_bitcompressg (x, y);
+}
+#endif
+
+#if __BITINT_MAXWIDTH__ >= 1027
+[[gnu::noipa]] unsigned _BitInt(1027)
+bitcompress1027 (unsigned _BitInt(1027) x, unsigned _BitInt(1027) y)
+{
+  return __builtin_bitcompressg (x, y);
+}
+#endif
+
+#ifdef __SIZEOF_INT128__
+#define T unsigned __int128
+#else
+#define T unsigned long long
+#endif
+
+[[gnu::noipa]] T
+bitcompress8w (T x, T m)
+{
+  return bitcompress8 (x, m);
+}
+
+[[gnu::noipa]] T
+bitcompress16w (T x, T m)
+{
+  return bitcompress16 (x, m);
+}
+
+[[gnu::noipa]] T
+bitcompress32w (T x, T m)
+{
+  return bitcompress32 (x, m);
+}
+
+[[gnu::noipa]] T
+bitcompresslw (T x, T m)
+{
+  return bitcompressl (x, m);
+}
+
+[[gnu::noipa]] T
+bitcompress64w (T x, T m)
+{
+  return bitcompress64 (x, m);
+}
+
+int
+main ()
+{
+  struct { T (*fn) (T, T); unsigned int digits; } tests[] = {
+    { bitcompress8w, __CHAR_BIT__ },
+    { bitcompress16w, __CHAR_BIT__ * __SIZEOF_SHORT__ },
+    { bitcompress32w, __CHAR_BIT__ * __SIZEOF_INT__ },
+    { bitcompresslw, __CHAR_BIT__ * __SIZEOF_LONG__ },
+    { bitcompress64w, __CHAR_BIT__ * __SIZEOF_LONG_LONG__ },
+#ifdef __SIZEOF_INT128__
+    { bitcompress128, __CHAR_BIT__ * __SIZEOF_INT128__ },
+#endif
+  };
+  for (int i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
+    {
+      if (tests[i].fn (0xff, 0xff) != 0xff)
+	__builtin_abort ();
+      if (tests[i].fn (0xff, 0x0f) != 0x0f)
+	__builtin_abort ();
+      if (tests[i].fn (0xff, 0xf0) != 0x0f)
+	__builtin_abort ();
+      if ((tests[i].digits % 2) == 0 && tests[i].digits <= 64)
+	{
+	  if (tests[i].fn (0xaaaaaaaaaaaaaaaaULL, 0xccccccccccccccccULL)
+	      != ((0xaaaaaaaaaaaaaaaaULL & (~0ULL >> (64 - tests[i].digits))) >> (tests[i].digits / 2)))
+	    __builtin_abort ();
+	  if (tests[i].fn (~0ULL, 0xaaaaaaaaaaaaaaaaULL)
+	      != ((~0ULL >> (64 - tests[i].digits)) >> (tests[i].digits / 2)))
+	    __builtin_abort ();
+	}
+      if (tests[i].fn (0xd4, 0xbf) != 0x54)
+	__builtin_abort ();
+      if (tests[i].fn (0x66, 0x1d) != 0x02)
+	__builtin_abort ();
+      if (tests[i].digits < 16)
+	continue;
+      if (tests[i].fn (0xebd4U, 0x93bfU) != 0x05d4U)
+	__builtin_abort ();
+      if (tests[i].fn (0xa166U, 0x001dU) != 2U)
+	__builtin_abort ();
+      if (tests[i].digits < 32)
+	continue;
+      if (tests[i].fn (0x9daeebd4UL, 0xad8793bfUL) != 0xbf5d4UL)
+	__builtin_abort ();
+      if (tests[i].fn (0x979ba166UL, 0x54ca001dUL) != 0x3b2UL)
+	__builtin_abort ();
+      if (tests[i].digits < 64)
+	continue;
+      if (tests[i].fn (0x5bd30f069daeebd4ULL, 0x64f9a89bad8793bfULL) != 0x13522bf5d4ULL)
+	__builtin_abort ();
+      if (tests[i].fn (0xb5672499979ba166ULL, 0xd315782354ca001dULL) != 0x2ad0bb2ULL)
+	__builtin_abort ();
+    }
+#if __BITINT_MAXWIDTH__ >= 256
+  {
+    const unsigned _BitInt(256) a = 2977712650221868789329368963712957747535884589227943441826207435705422199671uwb;
+    const unsigned _BitInt(256) b = 71392704391611725846964173788815479408492634097466601526619781877369006838376uwb;
+    if (bitcompress256 (a, b) != __builtin_bitcompressg (a, b)
+	|| bitcompress256 (-1, -1) != -1
+	|| bitcompress256 (0, 0) != 0
+	|| bitcompress256 (b, a) != __builtin_bitcompressg (b, a))
+      __builtin_abort ();
+  }
+#endif
+#if __BITINT_MAXWIDTH__ >= 575
+  {
+    const unsigned _BitInt(575) a = 118131541925402402155086157499115656285751034069848007471551618267237710886541403235442402972757841211601305512508765941610080588540585990221962870251502405139968332129617187uwb;
+    const unsigned _BitInt(575) b = 23115097167410578660280364356726488934202319035287675841474505237120386080336714513506555994051218240174733511200356850940493563860321836251689717522161899716121398557567847uwb;
+    if (bitcompress575 (a, b) != __builtin_bitcompressg (a, b)
+	|| bitcompress575 (-1, -1) != -1
+	|| bitcompress575 (0, 0) != 0
+	|| bitcompress575 (b, a) != __builtin_bitcompressg (b, a))
+      __builtin_abort ();
+  }
+#endif
+#if __BITINT_MAXWIDTH__ >= 1027
+  {
+    const unsigned _BitInt(1027) a = 294473855873745241271551195901386950656729759273365562206638967800992654698032636902920911003782368139677793592762040049443703439321090202240002644548914408256861393744782128292349394935235968569736234857665376806506152875996067378466482077335451170878303723908011096841794981692284316910893612604487249472311uwb;
+    const unsigned _BitInt(1027) b = 540548418560750924575664703872106986534265345396756980894364393357877823859721129540519729973560861792682263121263569358086814437568179760344168293628388053637218170474254474264275367843920704704472260655775742000715646811343262012513538736719387133499156091882497205971330308173485560590613388499248986333824uwb;
+    if (bitcompress1027 (a, b) != __builtin_bitcompressg (a, b)
+	|| bitcompress1027 (-1, -1) != -1
+	|| bitcompress1027 (0, 0) != 0
+	|| bitcompress1027 (b, a) != __builtin_bitcompressg (b, a))
+      __builtin_abort ();
+  }
+#endif
+}
--- a/gcc/testsuite/gcc.target/i386/bmi2-builtin-bitcompressg-1.c	2026-11-04 17:23:29.433451154 +0100
+++ b/gcc/testsuite/gcc.target/i386/bmi2-builtin-bitcompressg-1.c	2026-11-05 14:16:45.396231031 +0100
@@ -0,0 +1,15 @@
+/* { dg-do run } */
+/* { dg-require-effective-target bmi2 } */
+/* { dg-options "-O2 -mbmi2 -mno-popcnt" } */
+
+#include "bmi2-check.h"
+
+#define main do_main
+
+#include "../../c-c++-common/builtin-bitcompressg-1.c"
+
+static void
+bmi2_test (void)
+{
+  do_main ();
+}
--- a/gcc/testsuite/gcc.target/i386/bmi2-builtin-bitexpandg-1.c	2026-11-04 17:23:29.433550522 +0100
+++ b/gcc/testsuite/gcc.target/i386/bmi2-builtin-bitexpandg-1.c	2026-11-05 14:17:25.147699619 +0100
@@ -0,0 +1,15 @@
+/* { dg-do run } */
+/* { dg-require-effective-target bmi2 } */
+/* { dg-options "-O2 -mbmi2 -mno-popcnt" } */
+
+#include "bmi2-check.h"
+
+#define main do_main
+
+#include "../../c-c++-common/builtin-bitexpandg-1.c"
+
+static void
+bmi2_test (void)
+{
+  do_main ();
+}
--- a/gcc/testsuite/gcc.target/i386/pclmul-builtin-bitcompressg-1.c	2026-11-04 17:26:06.578736698 +0100
+++ b/gcc/testsuite/gcc.target/i386/pclmul-builtin-bitcompressg-1.c	2026-11-04 17:26:29.151437161 +0100
@@ -0,0 +1,19 @@
+/* { dg-do run } */
+/* { dg-require-effective-target pclmul } */
+/* { dg-options "-O2 -mpclmul -mno-bmi2" } */
+
+#ifndef CHECK_H
+#define CHECK_H "pclmul-check.h"
+#endif
+
+#include CHECK_H
+
+#define main do_main
+
+#include "../../c-c++-common/builtin-bitcompressg-1.c"
+
+static void
+pclmul_test (void)
+{
+  do_main ();
+}
--- a/gcc/testsuite/gcc.target/i386/pclmul-builtin-bitexpandg-1.c	2026-11-04 17:26:42.762256550 +0100
+++ b/gcc/testsuite/gcc.target/i386/pclmul-builtin-bitexpandg-1.c	2026-11-04 17:26:50.600152540 +0100
@@ -0,0 +1,19 @@
+/* { dg-do run } */
+/* { dg-require-effective-target pclmul } */
+/* { dg-options "-O2 -mpclmul -mno-bmi2" } */
+
+#ifndef CHECK_H
+#define CHECK_H "pclmul-check.h"
+#endif
+
+#include CHECK_H
+
+#define main do_main
+
+#include "../../c-c++-common/builtin-bitexpandg-1.c"
+
+static void
+pclmul_test (void)
+{
+  do_main ();
+}
--- a/gcc/testsuite/gcc.target/i386/bmi2-builtin-bitcompressg-2.c	2026-11-04 17:23:29.433451154 +0100
+++ b/gcc/testsuite/gcc.target/i386/bmi2-builtin-bitcompressg-2.c	2026-11-05 14:22:49.567362715 +0100
@@ -0,0 +1,19 @@
+/* { dg-do run } */
+/* { dg-require-effective-target bmi2 } */
+/* { dg-options "-O2 -mbmi2 -mpopcnt" } */
+
+#include "bmi2-check.h"
+
+#define main do_main
+
+#include "../../c-c++-common/builtin-bitcompressg-1.c"
+
+static void
+bmi2_test (void)
+{
+  unsigned int eax, ebx, ecx, edx;
+  if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
+    return;
+  if (ecx & bit_POPCNT)
+    do_main ();
+}
--- a/gcc/testsuite/gcc.target/i386/bmi2-builtin-bitexpandg-2.c	2026-11-04 17:23:29.433550522 +0100
+++ b/gcc/testsuite/gcc.target/i386/bmi2-builtin-bitexpandg-2.c	2026-11-05 14:22:55.897278091 +0100
@@ -0,0 +1,19 @@
+/* { dg-do run } */
+/* { dg-require-effective-target bmi2 } */
+/* { dg-options "-O2 -mbmi2 -mpopcnt" } */
+
+#include "bmi2-check.h"
+
+#define main do_main
+
+#include "../../c-c++-common/builtin-bitexpandg-1.c"
+
+static void
+bmi2_test (void)
+{
+  unsigned int eax, ebx, ecx, edx;
+  if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
+    return;
+  if (ecx & bit_POPCNT)
+    do_main ();
+}
--- a/gcc/internal-fn.cc	2026-11-03 11:23:44.307317463 +0100
+++ b/gcc/internal-fn.cc	2026-11-05 13:51:32.585449996 +0100
@@ -6349,3 +6349,466 @@ expand_CLMUL_WIDEN (internal_fn, gcall *
       write_complex_part (target, reshi, true, false);
     }
 }
+
+void
+expand_BITCOMPRESS (internal_fn, gcall *stmt)
+{
+  tree lhs = gimple_call_lhs (stmt);
+  tree arg0 = gimple_call_arg (stmt, 0);
+  tree arg1 = gimple_call_arg (stmt, 1);
+  scalar_int_mode mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg0));
+  unsigned int prec1 = get_min_precision (arg1, UNSIGNED);
+  rtx op0 = expand_normal (arg0);
+  rtx op1 = expand_normal (arg1);
+  rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
+  machine_mode cmode;
+  scalar_int_mode wmode = mode;
+  machine_mode clmul_mode = VOIDmode;
+  machine_mode half_mode = VOIDmode;
+  optab_methods olw = OPTAB_LIB_WIDEN;
+  if (prec1 == 1)
+    {
+      /* If mask operand has [0, 1] range, we can do just AND.  */
+      rtx tem = expand_binop (mode, and_optab, op0, op1, target, 1, olw);
+      if (tem != target)
+	emit_move_insn (target, tem);
+      return;
+    }
+  FOR_EACH_MODE_IN_CLASS (cmode, MODE_INT)
+    {
+      unsigned int this_prec
+	= GET_MODE_PRECISION (as_a <scalar_int_mode> (cmode));
+      if (prec1 > this_prec)
+	{
+	  if (this_prec * 2 >= prec1
+	      && optab_handler (bitcompress_optab, cmode) != CODE_FOR_nothing
+	      && optab_handler (popcount_optab, cmode) != CODE_FOR_nothing)
+	    half_mode = cmode;
+	  continue;
+	}
+      if (this_prec < GET_MODE_PRECISION (wmode))
+	wmode = as_a <scalar_int_mode> (cmode);
+      if (optab_handler (bitcompress_optab, cmode) != CODE_FOR_nothing)
+	{
+	  rtx_insn *start = get_last_insn ();
+	  rtx op0c = convert_modes (cmode, mode, op0, 1);
+	  rtx op1c = convert_modes (cmode, mode, op1, 1);
+	  if (rtx res = expand_binop (cmode, bitcompress_optab, op0c, op1c,
+				      NULL_RTX, 1, OPTAB_DIRECT))
+	    {
+	      res = convert_modes (mode, cmode, res, 1);
+	      emit_move_insn (target, res);
+	      return;
+	    }
+	  delete_insns_since (start);
+	}
+      if (this_prec < GET_MODE_PRECISION (wmode))
+	wmode = as_a <scalar_int_mode> (cmode);
+      if (optab_handler (clmul_optab, cmode) != CODE_FOR_nothing
+	  && (clmul_mode == VOIDmode
+	      || (this_prec
+		  < GET_MODE_PRECISION (as_a <scalar_int_mode> (clmul_mode)))))
+	clmul_mode = cmode;
+    }
+  /* If there is hw support for bitcompress for at least half as wide mode,
+     use one step of a recursive algorithm.  H being the half type.
+     T
+     bitcompressg (T x, T m)
+     {
+       int hprec = __builtin_popcountg ((H) ~(H) 0);
+       H x0 = x, x1 = x >> hprec;
+       H m0 = m, m1 = m >> hprec;
+       return bitcompressg (x0, m0)
+	      | (((T) bitcompressg (x1, m1)) << __builtin_popcountg (m0));
+     }  */
+  if (half_mode)
+    {
+      rtx_insn *start = get_last_insn ();
+      unsigned hprec = GET_MODE_PRECISION (as_a <scalar_int_mode> (half_mode));
+      rtx x0 = convert_modes (half_mode, mode, op0, 1);
+      rtx m0 = convert_modes (half_mode, mode, op1, 1);
+      rtx x1 = expand_shift (RSHIFT_EXPR, mode, op0, hprec, NULL_RTX, 1);
+      rtx m1 = expand_shift (RSHIFT_EXPR, mode, op1, hprec, NULL_RTX, 1);
+      x1 = convert_modes (half_mode, mode, x1, 1);
+      m1 = convert_modes (half_mode, mode, m1, 1);
+      if (rtx res1 = expand_binop (half_mode, bitcompress_optab, x0, m0,
+				   NULL_RTX, 1, OPTAB_DIRECT))
+	if (rtx res2 = expand_binop (half_mode, bitcompress_optab, x1, m1,
+				     NULL_RTX, 1, OPTAB_DIRECT))
+	  if (rtx res3 = expand_unop (half_mode, popcount_optab, m0,
+				      NULL_RTX, 1))
+	    {
+	      res1 = convert_modes (mode, half_mode, res1, 1);
+	      res2 = convert_modes (mode, half_mode, res2, 1);
+	      res3 = convert_modes (TYPE_MODE (integer_type_node),
+				    GET_MODE (res3), res3, 1);
+	      res2 = expand_variable_shift (LSHIFT_EXPR, mode, res2,
+					    make_tree (integer_type_node,
+						       res3), NULL_RTX, 1);
+	      res1 = expand_binop (mode, ior_optab, res1, res2, target, 1,
+				   olw);
+	      if (res1 != target)
+		emit_move_insn (target, res1);
+	      return;
+	    }
+      delete_insns_since (start);
+    }
+  /* Fallback based on "Compress, or Generalized Extract" in Hacker's Delight.
+     T
+     bitcompressg (T x, T m)
+     {
+       int prec = __builtin_popcountg ((T) ~(T) 0);
+       x &= m;
+       T mk = ~m << 1;
+       for (int i = 1; i < prec; i <<= 1)
+	 {
+	   T mp = mk;
+	   for (int j = 1; j < prec; j <<= 1)
+	     mp ^= mp << j;
+	   // If there is __builtin_clmulg hw support for T,
+	   // the above 3 lines can be done using
+	   // T mp = __builtin_clmulg (mk, (T) -1);
+	   // instead.
+	   T mv = mp & m;
+	   m = (m ^ mv) | (mv >> i);
+	   T t = x & mv;
+	   x = (x ^ t) | (t >> i);
+	   mk = mk & ~mp;
+	 }
+       return x;
+     }  */
+  op0 = convert_modes (wmode, mode, op0, 1);
+  op1 = convert_modes (wmode, mode, op1, 1);
+  rtx x = gen_reg_rtx (wmode);
+  rtx tem = expand_binop (wmode, and_optab, op0, op1, x, 1, olw);
+  if (tem != x)
+    emit_move_insn (x, tem);
+  rtx m = gen_reg_rtx (wmode);
+  emit_move_insn (m, op1);
+  tem = expand_unop (wmode, one_cmpl_optab, op1, NULL_RTX, 1);
+  rtx mk = gen_reg_rtx (wmode);
+  tem = expand_shift (LSHIFT_EXPR, wmode, tem, 1, mk, 1);
+  if (tem != mk)
+    emit_move_insn (mk, tem);
+  rtx i = gen_reg_rtx (TYPE_MODE (integer_type_node));
+  emit_move_insn (i, const1_rtx);
+  rtx_code_label *loop1_label = gen_label_rtx ();
+  emit_label (loop1_label);
+  rtx mp = gen_reg_rtx (wmode);
+  rtx mpval = NULL_RTX;
+  if (clmul_mode != VOIDmode)
+    {
+      rtx_insn *start = get_last_insn ();
+      tem = convert_modes (clmul_mode, wmode, mk, 1);
+      rtx m1 = convert_modes (clmul_mode, wmode, constm1_rtx, 1);
+      if (rtx res = expand_binop (clmul_mode, clmul_optab, tem, m1,
+				  NULL_RTX, 1, OPTAB_DIRECT))
+	mpval = convert_modes (wmode, clmul_mode, res, 1);
+      else
+	delete_insns_since (start);
+    }
+  if (mpval == NULL_RTX)
+    {
+      emit_move_insn (mp, mk);
+      rtx j = gen_reg_rtx (TYPE_MODE (integer_type_node));
+      emit_move_insn (j, const1_rtx);
+      rtx_code_label *loop2_label = gen_label_rtx ();
+      emit_label (loop2_label);
+      tem = expand_variable_shift (LSHIFT_EXPR, wmode, mp,
+				   make_tree (integer_type_node, j),
+				   NULL_RTX, 1);
+      tem = expand_binop (wmode, xor_optab, mp, tem, mp, 1, olw);
+      if (tem != mp)
+	emit_move_insn (mp, tem);
+      tem = expand_binop (GET_MODE (j), add_optab, j, j, j, 1, olw);
+      if (tem != j)
+	emit_move_insn (j, tem);
+      do_compare_rtx_and_jump (j, gen_int_mode (prec1, GET_MODE (j)),
+			       LTU, true, GET_MODE (j), NULL_RTX, NULL,
+			       loop2_label,
+			       profile_probability::very_likely ());
+    }
+  else
+    emit_move_insn (mp, mpval);
+  rtx mv = expand_binop (wmode, and_optab, mp, m, NULL_RTX, 1, olw);
+  tem = expand_binop (wmode, xor_optab, m, mv, NULL_RTX, 1, olw);
+  tree it = make_tree (integer_type_node, i);
+  rtx tem2 = expand_variable_shift (RSHIFT_EXPR, wmode, mv, it, NULL_RTX, 1);
+  tem = expand_binop (wmode, ior_optab, tem, tem2, m, 1, olw);
+  if (tem != m)
+    emit_move_insn (m, tem);
+  rtx t = expand_binop (wmode, and_optab, x, mv, NULL_RTX, 1, olw);
+  tem = expand_binop (wmode, xor_optab, x, t, NULL_RTX, 1, olw);
+  tem2 = expand_variable_shift (RSHIFT_EXPR, wmode, t, it, NULL_RTX, 1);
+  tem = expand_binop (wmode, ior_optab, tem, tem2, x, 1, olw);
+  if (tem != x)
+    emit_move_insn (x, tem);
+  tem = expand_unop (wmode, one_cmpl_optab, mp, NULL_RTX, 1);
+  tem = expand_binop (wmode, and_optab, mk, tem, mk, 1, olw);
+  if (tem != mk)
+    emit_move_insn (mk, tem);
+  tem = expand_binop (GET_MODE (i), add_optab, i, i, i, 1, olw);
+  if (tem != i)
+    emit_move_insn (i, tem);
+  do_compare_rtx_and_jump (i, gen_int_mode (prec1, GET_MODE (i)),
+			   LTU, true, GET_MODE (i), NULL_RTX, NULL,
+			   loop1_label, profile_probability::very_likely ());
+  emit_move_insn (target, convert_modes (mode, wmode, x, 1));
+}
+
+void
+expand_BITEXPAND (internal_fn, gcall *stmt)
+{
+  tree lhs = gimple_call_lhs (stmt);
+  tree arg0 = gimple_call_arg (stmt, 0);
+  tree arg1 = gimple_call_arg (stmt, 1);
+  scalar_int_mode mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg0));
+  unsigned int prec1 = get_min_precision (arg1, UNSIGNED);
+  rtx op0 = expand_normal (arg0);
+  rtx op1 = expand_normal (arg1);
+  rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
+  machine_mode cmode;
+  scalar_int_mode wmode = mode;
+  machine_mode clmul_mode = VOIDmode;
+  machine_mode half_mode = VOIDmode;
+  optab_methods olw = OPTAB_LIB_WIDEN;
+  if (prec1 == 1)
+    {
+      /* If mask operand has [0, 1] range, we can do just AND.  */
+      rtx tem = expand_binop (mode, and_optab, op0, op1, target, 1, olw);
+      if (tem != target)
+	emit_move_insn (target, tem);
+      return;
+    }
+  FOR_EACH_MODE_IN_CLASS (cmode, MODE_INT)
+    {
+      unsigned int this_prec
+	= GET_MODE_PRECISION (as_a <scalar_int_mode> (cmode));
+      if (prec1 > this_prec)
+	{
+	  if (this_prec * 2 >= prec1
+	      && optab_handler (bitexpand_optab, cmode) != CODE_FOR_nothing
+	      && optab_handler (popcount_optab, cmode) != CODE_FOR_nothing)
+	    half_mode = cmode;
+	  continue;
+	}
+      if (this_prec < GET_MODE_PRECISION (wmode))
+	wmode = as_a <scalar_int_mode> (cmode);
+      if (optab_handler (bitexpand_optab, cmode) != CODE_FOR_nothing)
+	{
+	  rtx_insn *start = get_last_insn ();
+	  rtx op0c = convert_modes (cmode, mode, op0, 1);
+	  rtx op1c = convert_modes (cmode, mode, op1, 1);
+	  if (rtx res = expand_binop (cmode, bitexpand_optab, op0c, op1c,
+				      NULL_RTX, 1, OPTAB_DIRECT))
+	    {
+	      res = convert_modes (mode, cmode, res, 1);
+	      emit_move_insn (target, res);
+	      return;
+	    }
+	  delete_insns_since (start);
+	}
+      if (this_prec < GET_MODE_PRECISION (wmode))
+	wmode = as_a <scalar_int_mode> (cmode);
+      if (optab_handler (clmul_optab, cmode) != CODE_FOR_nothing
+	  && (clmul_mode == VOIDmode
+	      || (this_prec
+		  < GET_MODE_PRECISION (as_a <scalar_int_mode> (clmul_mode)))))
+	clmul_mode = cmode;
+    }
+  /* If there is hw support for bitexpand for at least half as wide mode,
+     use one step of a recursive algorithm.  H being the half type.
+     T
+     bitexpandg (T x, T m)
+     {
+       int hprec = __builtin_popcountg ((H) ~(H) 0);
+       H m0 = m, m1 = m >> hprec;
+       int k = __builtin_popcountg (m0);
+       H x0 = x, x1 = x >> k;
+       return bitexpandg (x0, m0)
+	      | (((T) bitexpandg (x1, m1)) << hprec);
+     }  */
+  if (half_mode)
+    {
+      rtx_insn *start = get_last_insn ();
+      unsigned hprec = GET_MODE_PRECISION (as_a <scalar_int_mode> (half_mode));
+      rtx x0 = convert_modes (half_mode, mode, op0, 1);
+      rtx m0 = convert_modes (half_mode, mode, op1, 1);
+      if (rtx res3 = expand_unop (half_mode, popcount_optab, m0, NULL_RTX, 1))
+	{
+	  res3 = convert_modes (TYPE_MODE (integer_type_node),
+				GET_MODE (res3), res3, 1);
+	  rtx x1 = expand_variable_shift (RSHIFT_EXPR, mode, op0,
+					  make_tree (integer_type_node,
+						     res3), NULL_RTX, 1);
+	  rtx m1 = expand_shift (RSHIFT_EXPR, mode, op1, hprec, NULL_RTX, 1);
+	  x1 = convert_modes (half_mode, mode, x1, 1);
+	  m1 = convert_modes (half_mode, mode, m1, 1);
+	  if (rtx res1 = expand_binop (half_mode, bitexpand_optab, x0, m0,
+				       NULL_RTX, 1, OPTAB_DIRECT))
+	    if (rtx res2 = expand_binop (half_mode, bitexpand_optab, x1, m1,
+					 NULL_RTX, 1, OPTAB_DIRECT))
+	      {
+		res1 = convert_modes (mode, half_mode, res1, 1);
+		res2 = convert_modes (mode, half_mode, res2, 1);
+		res2 = expand_shift (LSHIFT_EXPR, mode, res2, hprec,
+				     NULL_RTX, 1);
+		res1 = expand_binop (mode, ior_optab, res1, res2, target, 1,
+				     olw);
+		if (res1 != target)
+		  emit_move_insn (target, res1);
+		return;
+	      }
+	}
+      delete_insns_since (start);
+    }
+  /* Fallback based on "Expand, or Generalized Insert" in Hacker's Delight.
+     T
+     bitexpandg (T x, T m)
+     {
+       int prec = __builtin_popcountg ((T) ~(T) 0);
+       int log2prec = ceil_log2 (prec);
+       T m0 = m;
+       T mk = ~m << 1;
+       T array[log2prec];
+       for (int i = 0; i < log2prec; i++)
+	 {
+	   T mp = mk;
+	   for (int j = 1; j < prec; j <<= 1)
+	     mp ^= mp << j;
+	   T mv = mp & m;
+	   array[i] = mv;
+	   m = (m ^ mv) | (mv >> (1 << i));
+	   mk = mk & ~mp;
+	 }
+       for (int i = log2prec - 1; i >= 0; i--)
+	 {
+	   T mv = array[i];
+	   T t = x << (((T) 1) << i);
+	   x = (x & ~mv) | (t & mv);
+	 }
+       return x & m0;
+     }  */
+  op0 = convert_modes (wmode, mode, op0, 1);
+  op1 = convert_modes (wmode, mode, op1, 1);
+  rtx x = gen_reg_rtx (wmode);
+  emit_move_insn (x, op0);
+  rtx m = gen_reg_rtx (wmode);
+  emit_move_insn (m, op1);
+  rtx m0 = op1;
+  rtx tem = expand_unop (wmode, one_cmpl_optab, op1, NULL_RTX, 1);
+  rtx mk = gen_reg_rtx (wmode);
+  tem = expand_shift (LSHIFT_EXPR, wmode, tem, 1, mk, 1);
+  if (tem != mk)
+    emit_move_insn (mk, tem);
+  unsigned log2_prec1 = ceil_log2 (prec1);
+  rtx array = assign_stack_temp (wmode, log2_prec1 * GET_MODE_SIZE (wmode));
+  rtx array_addr = force_reg (GET_MODE (XEXP (array, 0)), XEXP (array, 0));
+  rtx i = gen_reg_rtx (TYPE_MODE (integer_type_node));
+  emit_move_insn (i, const0_rtx);
+  rtx i2 = gen_reg_rtx (TYPE_MODE (integer_type_node));
+  emit_move_insn (i2, const1_rtx);
+  rtx_code_label *loop1_label = gen_label_rtx ();
+  emit_label (loop1_label);
+  rtx mp = gen_reg_rtx (wmode);
+  rtx mpval = NULL_RTX;
+  if (clmul_mode != VOIDmode)
+    {
+      rtx_insn *start = get_last_insn ();
+      tem = convert_modes (clmul_mode, wmode, mk, 1);
+      rtx m1 = convert_modes (clmul_mode, wmode, constm1_rtx, 1);
+      if (rtx res = expand_binop (clmul_mode, clmul_optab, tem, m1,
+				  NULL_RTX, 1, OPTAB_DIRECT))
+	mpval = convert_modes (wmode, clmul_mode, res, 1);
+      else
+	delete_insns_since (start);
+    }
+  if (mpval == NULL_RTX)
+    {
+      emit_move_insn (mp, mk);
+      rtx j = gen_reg_rtx (TYPE_MODE (integer_type_node));
+      emit_move_insn (j, const1_rtx);
+      rtx_code_label *loop2_label = gen_label_rtx ();
+      emit_label (loop2_label);
+      tem = expand_variable_shift (LSHIFT_EXPR, wmode, mp,
+				   make_tree (integer_type_node, j),
+				   NULL_RTX, 1);
+      tem = expand_binop (wmode, xor_optab, mp, tem, mp, 1, olw);
+      if (tem != mp)
+	emit_move_insn (mp, tem);
+      tem = expand_binop (GET_MODE (j), add_optab, j, j, j, 1, olw);
+      if (tem != j)
+	emit_move_insn (j, tem);
+      do_compare_rtx_and_jump (j, gen_int_mode (prec1, GET_MODE (j)),
+			       LTU, true, GET_MODE (j), NULL_RTX, NULL,
+			       loop2_label,
+			       profile_probability::very_likely ());
+    }
+  else
+    emit_move_insn (mp, mpval);
+  rtx mv = expand_binop (wmode, and_optab, mp, m, NULL_RTX, 1, olw);
+  tem = convert_modes (GET_MODE (array_addr), GET_MODE (i), i, 1);
+  if (GET_MODE_SIZE (wmode) != 1)
+    tem = expand_mult (GET_MODE (array_addr), tem,
+		       GEN_INT (GET_MODE_SIZE (wmode)),
+		       NULL_RTX, 1, false);
+  tem = expand_binop (GET_MODE (array_addr), add_optab, array_addr, tem,
+		      NULL_RTX, 1, olw);
+  tem = change_address (array, wmode, tem);
+  emit_move_insn (tem, mv);
+  tem = expand_binop (wmode, xor_optab, m, mv, NULL_RTX, 1, olw);
+  tree i2t = make_tree (integer_type_node, i2);
+  rtx tem2 = expand_variable_shift (RSHIFT_EXPR, wmode, mv, i2t, NULL_RTX, 1);
+  tem = expand_binop (wmode, ior_optab, tem, tem2, m, 1, olw);
+  if (tem != m)
+    emit_move_insn (m, tem);
+  tem = expand_unop (wmode, one_cmpl_optab, mp, NULL_RTX, 1);
+  tem = expand_binop (wmode, and_optab, mk, tem, mk, 1, olw);
+  if (tem != mk)
+    emit_move_insn (mk, tem);
+  tem = expand_binop (GET_MODE (i), add_optab, i, const1_rtx, i, 1, olw);
+  if (tem != i)
+    emit_move_insn (i, tem);
+  tem = expand_binop (GET_MODE (i2), add_optab, i2, i2, i2, 1, olw);
+  if (tem != i2)
+    emit_move_insn (i2, tem);
+  do_compare_rtx_and_jump (i, gen_int_mode (log2_prec1, GET_MODE (i)),
+			   LTU, true, GET_MODE (i), NULL_RTX, NULL,
+			   loop1_label, profile_probability::very_likely ());
+  tem = expand_binop (GET_MODE (i), add_optab, i, constm1_rtx, i, 1, olw);
+  if (tem != i)
+    emit_move_insn (i, tem);
+  tem = expand_shift (RSHIFT_EXPR, GET_MODE (i2), i2, 1, i2, 1);
+  if (tem != i2)
+    emit_move_insn (i2, tem);
+  rtx_code_label *loop3_label = gen_label_rtx ();
+  emit_label (loop3_label);
+  tem = convert_modes (GET_MODE (array_addr), GET_MODE (i), i, 1);
+  if (GET_MODE_SIZE (wmode) != 1)
+    tem = expand_mult (GET_MODE (array_addr), tem,
+		       GEN_INT (GET_MODE_SIZE (wmode)),
+		       NULL_RTX, 1, false);
+  tem = expand_binop (GET_MODE (array_addr), add_optab, array_addr, tem,
+		      NULL_RTX, 1, olw);
+  tem = change_address (array, wmode, tem);
+  mv = force_reg (wmode, tem);
+  rtx t = expand_variable_shift (LSHIFT_EXPR, wmode, x, i2t, NULL_RTX, 1);
+  tem = expand_unop (wmode, one_cmpl_optab, mv, NULL_RTX, 1);
+  tem = expand_binop (wmode, and_optab, x, tem, NULL_RTX, 1, olw);
+  tem2 = expand_binop (wmode, and_optab, t, mv, NULL_RTX, 1, olw);
+  tem = expand_binop (wmode, ior_optab, tem, tem2, x, 1, olw);
+  if (tem != x)
+    emit_move_insn (x, tem);
+  tem = expand_binop (GET_MODE (i), add_optab, i, constm1_rtx, i, 1, olw);
+  if (tem != i)
+    emit_move_insn (i, tem);
+  tem = expand_shift (RSHIFT_EXPR, GET_MODE (i2), i2, 1, i2, 1);
+  if (tem != i2)
+    emit_move_insn (i2, tem);
+  do_compare_rtx_and_jump (i, const0_rtx, GE, false, GET_MODE (i), NULL_RTX,
+			   NULL, loop3_label,
+			   profile_probability::very_likely ());
+  tem = expand_binop (wmode, and_optab, x, m0, NULL_RTX, 1, olw);
+  tem = convert_modes (mode, wmode, tem, 1);
+  emit_move_insn (target, tem);
+}
--- a/gcc/internal-fn.def	2026-11-03 11:23:44.306668169 +0100
+++ b/gcc/internal-fn.def	2026-11-04 17:23:29.409156034 +0100
@@ -551,6 +551,8 @@ DEF_INTERNAL_FN (VEC_CONVERT, ECF_CONST
 DEF_INTERNAL_FN (RAWMEMCHR, ECF_PURE | ECF_LEAF | ECF_NOTHROW, NULL)
 DEF_INTERNAL_FN (CLMUL, ECF_CONST | ECF_LEAF | ECF_NOTHROW, NULL)
 DEF_INTERNAL_FN (CLMUL_WIDEN, ECF_CONST | ECF_LEAF | ECF_NOTHROW, NULL)
+DEF_INTERNAL_FN (BITCOMPRESS, ECF_CONST | ECF_LEAF | ECF_NOTHROW, NULL)
+DEF_INTERNAL_FN (BITEXPAND, ECF_CONST | ECF_LEAF | ECF_NOTHROW, NULL)
 
 /* An unduplicable, uncombinable function.  Generally used to preserve
    a CFG property in the face of jump threading, tail merging or
--- a/gcc/internal-fn.h	2026-11-03 11:23:44.300626204 +0100
+++ b/gcc/internal-fn.h	2026-11-04 17:23:29.432048583 +0100
@@ -290,6 +290,8 @@ extern void expand_BSWAP (internal_fn, g
 extern void expand_BITREVERSE (internal_fn, gcall *);
 extern void expand_CLMUL (internal_fn, gcall *);
 extern void expand_CLMUL_WIDEN (internal_fn, gcall *);
+extern void expand_BITCOMPRESS (internal_fn, gcall *);
+extern void expand_BITEXPAND (internal_fn, gcall *);
 
 extern bool vectorized_internal_fn_supported_p (internal_fn, tree);
 
--- a/libstdc++-v3/include/std/bit	2026-10-14 22:17:47.555108907 +0200
+++ b/libstdc++-v3/include/std/bit	2026-11-04 18:02:20.848308476 +0100
@@ -572,7 +572,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     [[nodiscard]] constexpr _Tp
     bit_compress(_Tp __x, _Tp __m) noexcept
     {
-#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_elementwise_pext)
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_bitcompressg)
+      return __builtin_bitcompressg(__x, __m);
+#elif _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_elementwise_pext)
       return __builtin_elementwise_pext(__x, __m);
 #else
       constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
@@ -598,7 +600,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     [[nodiscard]] constexpr _Tp
     bit_expand(_Tp __x, _Tp __m) noexcept
     {
-#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_elementwise_pdep)
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_bitexpandg)
+      return __builtin_bitexpandg(__x, __m);
+#elif _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_elementwise_pdep)
       return __builtin_elementwise_pdep(__x, __m);
 #else
       constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;


	Jakub



More information about the Libstdc++ mailing list