The heuristic introduced for PR71016 prevents recognizing a max / min combo like it is used for
saturation when followed by a conversion.
The attached patch refines the heuristic to allow this case. Regression tested on x86_64-pc-linux-gnu .

Index: tree-ssa-phiopt.c
===================================================================
--- tree-ssa-phiopt.c   (revision 272846)
+++ tree-ssa-phiopt.c   (working copy)
@@ -504,7 +504,18 @@ factor_out_conditional_conversion (edge
                  gsi = gsi_for_stmt (arg0_def_stmt);
                  gsi_prev_nondebug (&gsi);
                  if (!gsi_end_p (gsi))
-                   return NULL;
+                   {
+                     gimple *assign = gsi_stmt (gsi);
+                     if (gimple_code (assign) != GIMPLE_ASSIGN)
+                       return NULL;
+                     tree lhs = gimple_assign_lhs (assign);
+                     enum tree_code ass_code = gimple_assign_rhs_code (assign);
+                     if (ass_code != MAX_EXPR && ass_code != MIN_EXPR)
+                       return NULL;
+                     gsi_prev_nondebug (&gsi);
+                     if (!gsi_end_p (gsi))
+                       return NULL;
+                   }
                  gsi = gsi_for_stmt (arg0_def_stmt);
                  gsi_next_nondebug (&gsi);
                  if (!gsi_end_p (gsi))
Index: testsuite/gcc.dg/tree-ssa/pr66726-4.c
===================================================================
--- testsuite/gcc.dg/tree-ssa/pr66726-4.c       (nonexistent)
+++ testsuite/gcc.dg/tree-ssa/pr66726-4.c       (working copy)
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-phiopt1-details" } */
+
+#define SAT(x) (x < 0 ? 0 : (x > 255 ? 255 : x))
+
+void
+foo (unsigned char *p, int i)
+{
+  *p = SAT (i);
+}
+
+/* { dg-final { scan-tree-dump-times "COND_EXPR .*and PHI .*converted to 
straightline code" 1 "phiopt1" } } */

Reply via email to