diff --git a/gcc/match.pd b/gcc/match.pd
index f08ab674a7064ddb41e73e42c9428ef2bd0c2ca1..e731bdba171e82dfdba3938ea51db5024b225a76 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3993,6 +3993,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
    (if (single_use (@2))
     (cmp @0 @1)))))
 
+/* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
+   (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
+(for cmp (lt ge)
+ (simplify
+  (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
+   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+	&& !TYPE_UNSIGNED (TREE_TYPE (@0))
+	&& types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
+    (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
+/* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
+   (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
+(simplify
+ (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
+  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+       && !TYPE_UNSIGNED (TREE_TYPE (@0))
+       && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
+   (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
+
 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
    signed arithmetic case.  That form is created by the compiler
    often enough for folding it to be of value.  One example is in
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr96681.c b/gcc/testsuite/gcc.dg/tree-ssa/pr96681.c
new file mode 100644
index 0000000000000000000000000000000000000000..6d72a1a3d7d83483f1761558952a3e58d434f9c6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr96681.c
@@ -0,0 +1,35 @@
+/* PR tree-optimization/96681 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump-times " \\^ " 5 "optimized" } } */
+/* { dg-final { scan-tree-dump-times " (?:<|>=) 0" 5 "optimized" } } */
+
+int
+foo (int x, int y)
+{
+  return (x < 0) ^ (y < 0);
+}
+
+int
+bar (int x, int y)
+{
+  return (x > -1) ^ (y > -1);
+}
+
+int
+baz (int x, int y)
+{
+  return (x ^ y) < 0;
+}
+
+int
+qux (int x, int y)
+{
+  return (x ^ y) >= 0;
+}
+
+int
+corge (int x, int y)
+{
+  return (x >= 0) ^ (y < 0);
+}