public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH][WIP] PR tree-optimization/101808 Boolean comparison simplification
@ 2021-11-23 18:34 Navid Rahimi
  2021-11-23 19:14 ` Jeff Law
  0 siblings, 1 reply; 11+ messages in thread
From: Navid Rahimi @ 2021-11-23 18:34 UTC (permalink / raw)
  To: Navid Rahimi via Gcc-patches

[-- Attachment #1: Type: text/plain, Size: 531 bytes --]

Hi GCC community,

I wanted you take a quick look at this patch to solve this bug [1]. This is the code example for the optimization [2] which does include a link to proof of each different optimization.

I think it should be possible to use simpler approach than what Andrew has used here [3].

P.S. Tested and verified on Linux x86_64.

1) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101808 
2) https://compiler-explorer.com/z/Gc448eE3z
3) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101808#c1

Best wishes,
Navid.

[-- Attachment #2: 0001-PR-tree-optimization-101808.patch --]
[-- Type: application/octet-stream, Size: 3144 bytes --]

From 30f8a30be575aaa69ccad8c8eec26ac7737c2e78 Mon Sep 17 00:00:00 2001
From: Navid Rahimi <navidrahimi@microsoft.com>
Date: Mon, 22 Nov 2021 16:46:44 -0800
Subject: [PATCH] PR tree-optimization/101807

	* match.pd (bool0 < bool1) -> (!bool0 & bool1): New optimization.
	* match.pd  (bool0 <= bool1) -> (!bool0 | bool1): New optimization.
	* match.pd  (bool0 > bool1) -> (bool0 & !bool1): New optimization.
	* match.pd  (bool0 >= bool1) -> (bool0 | !bool1): New optimization.
	* gcc.dg/tree-ssa/pr101807.c: testcase for this optimization.
---
 gcc/match.pd                             | 33 ++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr101807.c | 33 ++++++++++++++++++++++++
 2 files changed, 66 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr101807.c

diff --git a/gcc/match.pd b/gcc/match.pd
index a319aefa808..50ddb6d9e8c 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4449,6 +4449,39 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  )
 )
 
+/* (bool0 < bool1) -> (!bool0 & bool1) */
+(simplify
+ (lt truth_valued_p@0 truth_valued_p@1)
+ (if (TREE_CODE (type) == BOOLEAN_TYPE
+      && types_match (type, TREE_TYPE (@0))
+      && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
+  (bit_and (bit_not @0) @1)))
+
+/* (bool0 <= bool1) -> (!bool0 | bool1) */
+(simplify
+ (le truth_valued_p@0 truth_valued_p@1)
+ (if (TREE_CODE (type) == BOOLEAN_TYPE
+      && types_match (type, TREE_TYPE (@0))
+      && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
+  (bit_ior (bit_not @0) @1)))
+
+/* (bool0 > bool1) -> (bool0 & !bool1) */
+(simplify
+ (gt truth_valued_p@0 truth_valued_p@1)
+ (if (TREE_CODE (type) == BOOLEAN_TYPE
+      && types_match (type, TREE_TYPE (@0))
+      && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
+  (bit_and @0 (bit_not @1))))
+
+/* (bool0 >= bool1) -> (bool0 | !bool1) */
+(simplify
+ (ge truth_valued_p@0 truth_valued_p@1)
+ (if (TREE_CODE (type) == BOOLEAN_TYPE
+      && types_match (type, TREE_TYPE (@0))
+      && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
+  (bit_ior @0 (bit_not @1))))
+
+
 /* -(type)!A -> (type)A - 1.  */
 (simplify
  (negate (convert?:s (logical_inverted_value:s @0)))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr101807.c b/gcc/testsuite/gcc.dg/tree-ssa/pr101807.c
new file mode 100644
index 00000000000..29012ca1fb1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr101807.c
@@ -0,0 +1,33 @@
+/* PR tree-optimization/101807 */
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-optimized" } */
+
+#include <stdbool.h>
+
+bool g(bool a, bool b)
+{
+    return (a < b);
+}
+
+bool h(bool a, bool b)
+{
+  return (a <= b);
+}
+
+bool i(bool a, bool b)
+{
+  return (a > b);
+}
+
+bool j(bool a, bool b)
+{
+  return (a >= b);
+}
+
+
+/* Make sure we have removed < and <= from "a < b" and "a <= b". */
+/* { dg-final { scan-tree-dump-not " < " "optimized" } } */
+/* { dg-final { scan-tree-dump-not " <= " "optimized" } } */
+/* Make sure we have removed > and >= from "a > b" and "a >= b". */
+/* { dg-final { scan-tree-dump-not " > " "optimized" } } */
+/* { dg-final { scan-tree-dump-not " >= " "optimized" } } */
\ No newline at end of file
-- 
2.25.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2021-12-03 15:43 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-23 18:34 [PATCH][WIP] PR tree-optimization/101808 Boolean comparison simplification Navid Rahimi
2021-11-23 19:14 ` Jeff Law
2021-11-23 19:33   ` Andrew Pinski
2021-11-23 19:55     ` [EXTERNAL] " Navid Rahimi
2021-11-23 20:03       ` Jeff Law
2021-11-29 23:51         ` Navid Rahimi
2021-12-03 15:43           ` Jeff Law
2021-11-23 19:42   ` Navid Rahimi
2021-11-23 20:02     ` Jeff Law
2021-11-23 20:08       ` Navid Rahimi
2021-11-23 20:14         ` Jeff Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).