https://gcc.gnu.org/g:ed5f89a2062002f5dfb69aac8cb36c629d091105
commit r14-12292-ged5f89a2062002f5dfb69aac8cb36c629d091105 Author: Richard Biener <[email protected]> Date: Fri Jan 30 15:32:39 2026 +0100 middle-end/123887 - trapping conditional operand turned unconditional The following properly checks expr_no_side_effects_p on two patterns that turns a conditionally evaluated operand into unconditonal. PR middle-end/123887 * match.pd ((zero_one ==/!= 0) ? .. z <op> y .. -> zero_one * z ..): Check evaluating z unconditionally has no side-effects, like trapping. * gcc.dg/torture/pr123887.c: New testcase. (cherry picked from commit c1fa15791ae458e1bd8db6842dfc021230521a65) Diff: --- gcc/match.pd | 6 ++++-- gcc/testsuite/gcc.dg/torture/pr123887.c | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/gcc/match.pd b/gcc/match.pd index 60aa87e3d37c..1d5a90e33177 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4249,7 +4249,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (op:c @2 @1)) (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) > 1 - && (INTEGRAL_TYPE_P (TREE_TYPE (@0)))) + && INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && expr_no_side_effects_p (@2)) (op (mult (convert:type @0) @2) @1)))) /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */ @@ -4261,7 +4262,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) @1) (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) > 1 - && (INTEGRAL_TYPE_P (TREE_TYPE (@0)))) + && INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && expr_no_side_effects_p (@2)) (op (mult (convert:type @0) @2) @1)))) /* ?: Value replacement. */ diff --git a/gcc/testsuite/gcc.dg/torture/pr123887.c b/gcc/testsuite/gcc.dg/torture/pr123887.c new file mode 100644 index 000000000000..e354a1777a7d --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr123887.c @@ -0,0 +1,14 @@ +/* { dg-do run } */ + +typedef struct { + unsigned s; +} struct_t; + +struct_t *p; +_Bool cond; +int v = 1; +int main() +{ + int u = 1 + (cond ? p->s : 0); + return u - v; +}
