When I added this pattern in
r14-1411-g17cca3c43e2f49, I had missed the :c on the cmp
part of the pattern meaning there might be some missing optimizations happening.
The testcase shows an example of the missed optmization.
Committed as obvious after a bootstrap/test on x86_64-linux-gnu.
PR tree-optimization/111349
gcc/ChangeLog:
* match.pd (`(a CMP CST1) ? max<a,CST2> : a`): Add :c on
the cmp part of the pattern.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/minmax-25.c: New test.
(for cmp (gt ge lt le)
minmax (min min max max)
(simplify
- (cond (cmp @0 @1) (minmax:c@2 @0 @3) @4)
+ (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
(with
{
tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-original" } */
+/* PR tree-optimization/111349 */
+
+int f();
+int g();
+
+int test1(int a, int b)
+{
+ return (a > b) ? ((a > b) ? a : b) : a;
+}
+
+int test1_(int a, int b)
+{
+ return (b < a) ? ((a > b) ? a : b) : a;
+}
+
+/* test1 and test1_ should be able to optimize to `return a;` during fold. */
+/* { dg-final { scan-tree-dump-times "return a;" 2 "original" } } */
+/* { dg-final { scan-tree-dump-not " MAX_EXPR " "original" } } */
+/* { dg-final { scan-tree-dump-times "return a" 2 "optimized" } } */