(cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
(with
{
- tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
+ tree_code code = minmax_from_comparison (cmp, @1, @3, @1, @2);
}
(if (code == MIN_EXPR)
(minmax (min @1 @2) @4)
--- /dev/null
+/* { dg-do run } */
+/* PR tree-optimization/126456 */
+
+/* These should not produce min/max for
+ the outer conditional. */
+
+__attribute__((noipa)) int
+min_le (int a, int c)
+{
+ return (a <= 6) ? (a < c ? a : c) : (5 < c ? 5 : c);
+}
+
+__attribute__((noipa)) int
+max_ge (int a, int c)
+{
+ return (a >= 4) ? (a > c ? a : c) : (5 > c ? 5 : c);
+}
+
+int
+main (void)
+{
+ if (min_le (6, 10) != 6)
+ __builtin_abort ();
+ if (max_ge (4, 0) != 4)
+ __builtin_abort ();
+ return 0;
+}
+
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+/* These should not produce max, only 3 min and there should be an if left. */
+
+__attribute__((noipa)) int
+min_le (int a, int c)
+{
+ return (a <= 6) ? (a < c ? a : c) : (5 < c ? 5 : c);
+}
+
+int
+min_le_1 (int a, int c)
+{
+ if (a <= 6)
+ return (a < c ? a : c);
+ return (5 < c ? 5 : c);
+}
+
+int
+min_le_2 (int a, int c)
+{
+ int t = (a < c ? a : c);
+ int t1 = (5 < c ? 5 : c);
+ if (a <= 6)
+ return t;
+ return t1;
+}
+/* { dg-final { scan-tree-dump-not "MAX_EXPR " "optimized" } } */
+/* { dg-final { scan-tree-dump-times "MIN_EXPR " 3 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "if " 3 "optimized" } } */
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+/* These should not produce min, only 3 max and there should be an if left. */
+int
+max_ge (int a, int c)
+{
+ return (a >= 4) ? (a > c ? a : c) : (5 > c ? 5 : c);
+}
+
+int
+max_ge_1 (int a, int c)
+{
+ int t = (a > c ? a : c);
+ int t1 = (5 > c ? 5 : c);
+ return (a >= 4) ? t : t1;
+}
+
+int
+max_ge_2 (int a, int c)
+{
+ if (a >= 4)
+ return (a > c ? a : c);
+ return (5 > c ? 5 : c);
+}
+
+
+/* { dg-final { scan-tree-dump-times "MAX_EXPR " 3 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "if " 3 "optimized" } } */
+/* { dg-final { scan-tree-dump-not "MIN_EXPR " "optimized" } } */
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+/* These should produce 2x min for each function. */
+
+__attribute__((noipa)) int
+min_le (int a, int c)
+{
+ return (a < 5) ? (a < c ? a : c) : (5 < c ? 5 : c);
+}
+
+int
+min_le_1 (int a, int c)
+{
+ if (a < 5)
+ return (a < c ? a : c);
+ return (5 < c ? 5 : c);
+}
+
+int
+min_le_2 (int a, int c)
+{
+ int t = (a < c ? a : c);
+ int t1 = (5 < c ? 5 : c);
+ if (a < 5)
+ return t;
+ return t1;
+}
+/* { dg-final { scan-tree-dump-not "MAX_EXPR " "optimized" } } */
+/* { dg-final { scan-tree-dump-times "MIN_EXPR " 6 "optimized" } } */
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+/* These should produce 2x max for each function. */
+int
+max_ge (int a, int c)
+{
+ return (a > 4) ? (a > c ? a : c) : (5 > c ? 5 : c);
+}
+
+int
+max_ge_1 (int a, int c)
+{
+ int t = (a > c ? a : c);
+ int t1 = (5 > c ? 5 : c);
+ return (a > 4) ? t : t1;
+}
+
+int
+max_ge_2 (int a, int c)
+{
+ if (a > 4)
+ return (a > c ? a : c);
+ return (5 > c ? 5 : c);
+}
+
+
+/* { dg-final { scan-tree-dump-times "MAX_EXPR " 6 "optimized" } } */
+/* { dg-final { scan-tree-dump-not "MIN_EXPR " "optimized" } } */