@@ -1732,6 +1732,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(if (!FIXED_POINT_TYPE_P (type))
(plus @0 (negate @1))))
+#if GIMPLE
+/* 1 - a is a ^ 1 if a had a bool range. */
+/* This is only enabled for gimple as sometimes
+ cfun is not set for the function which contains
+ the SSA_NAME (e.g. while IPA passes are happening,
+ fold might be called). */
+(simplify
+ (minus integer_onep@0 SSA_NAME@1)
+ (if (INTEGRAL_TYPE_P (type)
+ && ssa_name_has_boolean_range (@1))
+ (bit_xor @1 @0)))
+#endif
+
/* Other simplifications of negation (c.f. fold_negate_expr_1). */
(simplify
(negate (mult:c@0 @1 negate_expr_p@2))
new file mode 100644
@@ -0,0 +1,11 @@
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+_Bool
+foo (_Bool a)
+{
+ int c = 1 - a;
+ return c;
+}
+
+/* { dg-final { scan-tree-dump-times "1 - " 0 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "~a" 1 "optimized" } } */
+
new file mode 100644
@@ -0,0 +1,33 @@
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+_Bool
+foo (_Bool a, _Bool b)
+{
+ int c = 1 - a;
+ int d = 1 - b;
+ int e = c & d;
+ return 1 - e;
+}
+
+_Bool
+bar (_Bool a, _Bool b)
+{
+ int c = 1 - a;
+ int d = 1 - b;
+ _Bool e = c & d;
+ return 1 - e;
+}
+
+_Bool
+baz (_Bool a, _Bool b)
+{
+ _Bool c = 1 - a;
+ _Bool d = 1 - b;
+ _Bool e = c & d;
+ return 1 - e;
+}
+
+/* { dg-final { scan-tree-dump-times "1 - " 0 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "~a" 0 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "~b" 0 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "a_\[0-9\]+.D. \\\| b_\[0-9\]+.D." 3 "optimized" } } */
+
new file mode 100644
@@ -0,0 +1,26 @@
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int b;
+int *c;
+int e;
+static int *f = &e;
+int g;
+void foo();
+short(a)(short h, short i) { return h - i; }
+int(d)(int h) { return h == 83647 ? 0 : -h; }
+int main() {
+ short j;
+ int *k = &e, *l = &b;
+ *f = 0 == c;
+ j = a(0 != 2, *k);
+ if (d(j ^ (0 == l || *k)) != *k)
+ ;
+ else
+ foo();
+ c = &g;
+}
+
+/* { dg-final { scan-tree-dump-times " 1 - " 0 "optimized" } } */
+/* There should be no calls to foo. */
+/* { dg-final { scan-tree-dump-times "foo " 0 "optimized" } } */
+