(if (code == MAX_EXPR)
(minmax (max @1 @2) @4)))))))
+/* Optimize (a CMP CST1) ? max<a,CST2> : a */
+(for cmp (gt ge lt le)
+ minmax (min min max max)
+ (simplify
+ (cond (cmp @0 @1) (minmax:c@2 @0 @3) @4)
+ (with
+ {
+ tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
+ }
+ (if ((cmp == LT_EXPR || cmp == LE_EXPR)
+ && code == MIN_EXPR
+ && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @1)))
+ (min @2 @4)
+ (if ((cmp == GT_EXPR || cmp == GE_EXPR)
+ && code == MAX_EXPR
+ && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @1)))
+ (max @2 @4))))))
+
/* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
(simplify
(cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
/* phiopt1 confused by predictors. */
/* { dg-final { scan-tree-dump "vary.*MAX_EXPR.*0\\.0" "phiopt1" } } */
-/* { dg-final { scan-tree-dump "vary.*MIN_EXPR.*1\\.0" "phiopt1" { xfail *-*-* } } } */
+/* { dg-final { scan-tree-dump "vary.*MIN_EXPR.*1\\.0" "phiopt1" } } */
/* { dg-final { scan-tree-dump "vary.*MAX_EXPR.*0\\.0" "phiopt2"} } */
/* { dg-final { scan-tree-dump "vary.*MIN_EXPR.*1\\.0" "phiopt2"} } */
*p = SAT (i);
}
-/* { dg-final { scan-tree-dump-times "COND_EXPR .*and PHI .*converted to straightline code" 1 "phiopt1" } } */
+/* fold could optimize SAT before phiopt1 so only match on the
+ MIN/MAX here. */
+/* { dg-final { scan-tree-dump-times "= MIN_EXPR" 1 "phiopt1" } } */
+/* { dg-final { scan-tree-dump-times "= MAX_EXPR" 1 "phiopt1" } } */
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-phiopt1-details -fdump-tree-phiopt2-details -fdump-tree-optimized" } */
+
+#define SAT(x) (x < 0 ? 0 : (x > 255 ? 255 : x))
+
+unsigned char
+foo (unsigned char *p, int i)
+{
+ if (i < 0)
+ return 0;
+ {
+ int t;
+ if (i > 255)
+ t = 255;
+ else
+ t = i;
+ return t;
+ }
+}
+
+/* Because of the way PHIOPT works, it only does the merging of BBs after it is done so we get the case were we can't
+ optimize the above until phiopt2 right now. */
+/* { dg-final { scan-tree-dump-times "COND_EXPR .*and PHI .*converted to straightline code" 2 "phiopt1" { xfail *-*-* } } } */
+/* { dg-final { scan-tree-dump-times "COND_EXPR .*and PHI .*converted to straightline code" 0 "phiopt2" { xfail *-*-* } } } */
+/* { dg-final { scan-tree-dump-times "= MIN_EXPR" 1 "phiopt1" } } */
+/* { dg-final { scan-tree-dump-times "= MAX_EXPR" 1 "phiopt1" { xfail *-*-* } } } */
+/* { dg-final { scan-tree-dump-times "= MIN_EXPR" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "= MAX_EXPR" 1 "optimized" } } */
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-phiopt1-details" } */
+
+
+unsigned char
+foo1 (unsigned char *p, int i)
+{
+ if (i < 0)
+ return 0;
+ {
+ int t = i > 255 ? 255 : i;
+ return t;
+ }
+}
+/* testing to see if moving the cast out of the conditional. */
+
+/* { dg-final { scan-tree-dump-times "COND_EXPR .*and PHI .*converted to straightline code" 1 "phiopt1" } } */