diff --git a/gcc/builtins.cc b/gcc/builtins.cc index a489ef1c564..6af2a0b07aa 100644 --- a/gcc/builtins.cc +++ b/gcc/builtins.cc @@ -9819,6 +9819,8 @@ fold_builtin_bit_query (location_t loc, enum built_in_function fcode, if (!direct_internal_fn_supported_p (ifn, arg0_type, OPTIMIZE_FOR_BOTH)) arg2 = NULL_TREE; + if (arg2 == NULL_TREE) + arg0 = save_expr (arg0); } if (fcodei == END_BUILTINS || arg2) call = build_call_expr_internal_loc (loc, ifn, integer_type_node, diff --git a/gcc/testsuite/gcc.dg/torture/pr112639.c b/gcc/testsuite/gcc.dg/torture/pr112639.c new file mode 100644 index 00000000000..9450d88baea --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr112639.c @@ -0,0 +1,34 @@ +/* PR middle-end/112639 */ +/* { dg-do run } */ + +unsigned long long b = 0; + +int +foo (void) +{ + return __builtin_clzg (b++, __SIZEOF_LONG_LONG__ * __CHAR_BIT__); +} + +int +bar (void) +{ + return __builtin_ctzg (b++, __SIZEOF_LONG_LONG__ * __CHAR_BIT__); +} + +int +main () +{ + if (foo () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ || b != 1) + __builtin_abort (); + if (foo () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ - 1 || b != 2) + __builtin_abort (); + if (foo () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ - 2 || b != 3) + __builtin_abort (); + b = 0; + if (bar () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ || b != 1) + __builtin_abort (); + if (bar () != 0 || b != 2) + __builtin_abort (); + if (bar () != 1 || b != 3) + __builtin_abort (); +}