]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
middle-end/123887 - trapping conditional operand turned unconditional
authorRichard Biener <rguenther@suse.de>
Fri, 30 Jan 2026 14:32:39 +0000 (15:32 +0100)
committerRichard Biener <rguenther@suse.de>
Fri, 30 Jan 2026 18:21:58 +0000 (19:21 +0100)
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.

gcc/match.pd
gcc/testsuite/gcc.dg/torture/pr123887.c [new file with mode: 0644]

index 05e8ea7a229d55cbf3660b16a176e582c03a8911..9b2da2be4c545687d2293f36b2599522c7ebdef6 100644 (file)
@@ -5178,7 +5178,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 */
@@ -5190,7 +5191,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 (file)
index 0000000..c2a9659
--- /dev/null
@@ -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;
+}