]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
match.pd: (A | (convert?)(A != 0)) EQ|NE 0 -> A EQ|NE 0 [PR114969]
authorDaniel Barboza <daniel.barboza@oss.qualcomm.com>
Fri, 23 Jan 2026 16:10:30 +0000 (13:10 -0300)
committerDaniel Barboza <daniel.barboza@oss.qualcomm.com>
Fri, 6 Feb 2026 17:21:22 +0000 (14:21 -0300)
The NE variant is a gimple pattern that comes from the following C++
code:

bool result = (std::max(
                  (unsigned long long) 0, (unsigned long long) var_0))
               | ( var_0 ?  1 : 0);

PR tree-optimization/114969

gcc/ChangeLog:

* match.pd (`(A | (convert?)(A != 0)) EQ|NE 0`): New pattern.

gcc/testsuite/ChangeLog:

* g++.dg/pr114969.C: New test.

gcc/match.pd
gcc/testsuite/g++.dg/pr114969.C [new file with mode: 0644]

index f4732d39c517a8cc9ff41c9b1d18e3756dbec72e..8910591a04b3751f72519a6c88330856bbdb1470 100644 (file)
@@ -8368,6 +8368,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
        sign extension followed by AND with C will achieve the effect.  */
     (bit_and (convert @0) @1)))))
 
+/* (A | (convert?)(A != 0)) EQ|NE 0 -> A EQ|NE 0 (PR114969)
+
+   The NE variant of this pattern is produced by the C++ code:
+
+   (bool)(std::max (ulong_A, 0) | (ulong_A ? 1 : 0)).  */
+(for cmp (eq ne)
+ (simplify
+  (cmp (bit_ior:c @0 (convert? (ne @0 integer_zerop))) integer_zerop)
+  (cmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
+
 /* When the addresses are not directly of decls compare base and offset.
    This implements some remaining parts of fold_comparison address
    comparisons but still no complete part of it.  Still it is good
diff --git a/gcc/testsuite/g++.dg/pr114969.C b/gcc/testsuite/g++.dg/pr114969.C
new file mode 100644 (file)
index 0000000..4eca335
--- /dev/null
@@ -0,0 +1,16 @@
+// { dg-do compile { target c++17 } }
+// { dg-options "-O3 -fdump-tree-optimized" }
+
+#include <algorithm>
+
+bool result;
+
+void func (unsigned long long var_0) {
+    result = (std::max ((unsigned long long) 0, (unsigned long long) var_0)) | ( var_0 ?  1 : 0);
+}
+
+void func2 (unsigned long long var_0) {
+    result = !((std::max ((unsigned long long) 0, (unsigned long long) var_0)) | ( var_0 ?  1 : 0));
+}
+
+/* { dg-final { scan-tree-dump-times " \\\| " 0 "optimized" } } */
\ No newline at end of file