https://gcc.gnu.org/g:7ec15f8a0b357e33a9c93ad25e4401dd75b7d466
commit r17-1280-g7ec15f8a0b357e33a9c93ad25e4401dd75b7d466 Author: Andrew Pinski <[email protected]> Date: Wed Jun 3 17:18:56 2026 -0700 match: Disable `if (cond) (A | CST1) : (A & ~CST1)` pattern for non-GIMPLE [PR125588] r17-265-gf6f33ca83cb6b8 added a new pattern that converts: cond ? (A | CST1) : (A & ~CST1) into `(A & ~CST1) | (cond * CST1)` but fold (partly via fold_binary_op_with_conditional_arg) will turn that back into `cond ? (A | CST1) : (A & ~CST1)` in some cases. In those cases we get an infinite loop and a stack overflow crash. This fixes the problem by enabling this pattern for GIMPLE only. Pushed as obvious after bootstrap/test on x86_64-linux-gnu. PR middle-end/125588 gcc/ChangeLog: * match.pd (`if (cond) (A | CST1) : (A & ~CST1)`): Enable for GIMPLE only. gcc/testsuite/ChangeLog: * gcc.dg/pr125588-1.c: New test. Signed-off-by: Andrew Pinski <[email protected]> Diff: --- gcc/match.pd | 2 ++ gcc/testsuite/gcc.dg/pr125588-1.c | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/gcc/match.pd b/gcc/match.pd index 45ffe39604eb..6b70999fab5a 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -6314,6 +6314,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && INTEGRAL_TYPE_P (TREE_TYPE (@0))) (cond @1 (convert @2) (convert @3)))) +#if GIMPLE /* PR123967: if (cond) A | CST1 : A & ~CST1 Make both bitops unconditional: (A & ~CST1) | (cond * CST1). */ @@ -6326,6 +6327,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && TYPE_PRECISION (type) <= BITS_PER_WORD && wi::to_wide (@1) == ~wi::to_wide (@2)) (bit_ior @3 (mult (convert:type @4) (convert:type @1)))))) +#endif /* Simplification moved from fold_cond_expr_with_comparison. It may also be extended. */ diff --git a/gcc/testsuite/gcc.dg/pr125588-1.c b/gcc/testsuite/gcc.dg/pr125588-1.c new file mode 100644 index 000000000000..d5763e9d8a60 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr125588-1.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-w" } */ +/* PR middle-end/125588 */ +void f() { +unsigned short a = 8 <= 0 % 0 | + 7 >> (6 >> 48) % 1 ^ + 6 >> 8 <= 0 & + 7 >> (6 >> 48) % 1; +}
