/* x != ~x -> true */
(for cmp (eq ne)
(simplify
- (cmp:c @0 (bit_not @0))
- { constant_boolean_node (cmp == NE_EXPR, type); }))
+ (cmp:c (convert? @0) (convert? (maybe_bit_not @1)))
+ (with { bool wascmp; }
+ (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
+ && bitwise_inverted_equal_p (@0, @1, wascmp))
+ { constant_boolean_node (cmp == NE_EXPR, type); }))))
/* Fold ~X op ~Y as Y op X. */
(for cmp (simple_comparison)
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/118483 */
+/* { dg-final { scan-tree-dump-not "abort " "optimized" } } */
+
+
+/* The value of `l == e` is always false as it is
+ `(b == 0) == (b != 0)`. */
+
+int d;
+int f(int b)
+{
+ int e = b == 0;
+ d = e;
+ int l = b != 0;
+ if (l == e)
+ __builtin_abort ();
+}
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/118483 */
+/* { dg-final { scan-tree-dump-not "abort " "optimized" } } */
+
+
+/* The value of `l == e` is always false as it is
+ `(b == 0) == (b != 0)`. */
+
+int d;
+int f(int b)
+{
+ int e = b == 0;
+ d = e;
+ int l = !e;
+ if (l == e)
+ __builtin_abort ();
+}
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/118483 */
+/* { dg-final { scan-tree-dump "return 0;" "optimized" } } */
+
+/* This should optimize down to just `return 0;` */
+/* as `(short)a == ~(short)a` is always false. */
+int f(int a)
+{
+ short b = a;
+ int e = ~a;
+ short c = e;
+ return b == c;
+}
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/118483 */
+/* { dg-final { scan-tree-dump "return 0;" "optimized" } } */
+
+/* This should optimize down to just `return 0;` */
+/* as `a == 0` and `a != 0` are opposites. */
+int f(int a)
+{
+ return (a == 0) == (a != 0);
+}