aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Pinski <apinski@marvell.com>2023-08-06 13:57:35 -0700
committerAndrew Pinski <apinski@marvell.com>2023-08-07 07:47:03 -0700
commitb57bd27cb68fdbe5d9dcd571b1cb66f72b841290 (patch)
tree5dbd5787bcd13790b03216d03836c9da3dcbf10e
parentd8efc44d00ced7a45c2a1ea886e48f872cde9e0f (diff)
downloadgcc-b57bd27cb68fdbe5d9dcd571b1cb66f72b841290.zip
gcc-b57bd27cb68fdbe5d9dcd571b1cb66f72b841290.tar.gz
gcc-b57bd27cb68fdbe5d9dcd571b1cb66f72b841290.tar.bz2
MATCH: [PR109959] `(uns <= 1) & uns` could be optimized to `uns == 1`
I noticed while looking into some code generation of bitmap_single_bit_set_p, that sometimes: ``` if (uns > 1) return 0; return uns == 1; ``` Would not optimize down to just: ``` return uns == 1; ``` In this case, VRP likes to change `a == 1` into `(bool)a` if a has a range of [0,1] due to `a <= 1` side of the branch. We might end up with this similar code even without VRP, in the case of builtin-sprintf-warn-23.c (and Wrestrict.c), we had: ``` if (s < 0 || 1 < s) s = 0; ``` Which is the same as `s = ((unsigned)s) <= 1 ? s : 0`; So we should be able to catch that also. This adds 2 patterns to catch `(uns <= 1) & uns` and `(uns > 1) ? 0 : uns` and convert those into: `(convert) uns == 1`. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/109959 gcc/ChangeLog: * match.pd (`(a > 1) ? 0 : (cast)a`, `(a <= 1) & (cast)a`): New patterns. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/builtin-sprintf-warn-23.c: Remove xfail. * c-c++-common/Wrestrict.c: Update test and remove some xfail. * gcc.dg/tree-ssa/cmpeq-1.c: New test. * gcc.dg/tree-ssa/cmpeq-2.c: New test. * gcc.dg/tree-ssa/cmpeq-3.c: New test.
-rw-r--r--gcc/match.pd20
-rw-r--r--gcc/testsuite/c-c++-common/Wrestrict.c11
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-23.c2
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/cmpeq-1.c36
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/cmpeq-2.c32
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/cmpeq-3.c22
6 files changed, 117 insertions, 6 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index de54b17..9b4819e 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4902,6 +4902,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
)
)
+/* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
+ for unsigned types. */
+(simplify
+ (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
+ (if (TYPE_UNSIGNED (TREE_TYPE (@0))
+ && bitwise_equal_p (@0, @2))
+ (convert (eq @0 @1))
+ )
+)
+
+/* (a <= 1) & (cast)a is the same as (cast)(a == 1)
+ for unsigned types. */
+(simplify
+ (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
+ (if (TYPE_UNSIGNED (TREE_TYPE (@0))
+ && bitwise_equal_p (@0, @2))
+ (convert (eq @0 @1))
+ )
+)
+
(simplify
(cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
(switch
diff --git a/gcc/testsuite/c-c++-common/Wrestrict.c b/gcc/testsuite/c-c++-common/Wrestrict.c
index 9eb02bd..4d005a6 100644
--- a/gcc/testsuite/c-c++-common/Wrestrict.c
+++ b/gcc/testsuite/c-c++-common/Wrestrict.c
@@ -681,7 +681,7 @@ void test_strcpy_range (void)
ptrdiff_t r;
r = SR (0, 1);
- T (8, "0", a + r, a); /* { dg-warning "accessing between 1 and 2 bytes at offsets \\\[0, 1] and 0 overlaps up to 2 bytes at offset \\\[0, 1]" "strcpy" { xfail *-*-*} } */
+ T (8, "0", a + r, a); /* { dg-warning "accessing 2 bytes at offsets \\\[0, 1] and 0 overlaps between 1 and 2 bytes at offset \\\[0, 1]" "strcpy" } */
r = SR (2, 5);
T (8, "01", a + r, a); /* { dg-warning "accessing 3 bytes at offsets \\\[2, 5] and 0 may overlap 1 byte at offset 2" } */
@@ -860,10 +860,11 @@ void test_strncpy_range (char *d, size_t n)
i = SR (0, 1);
T ("0123", a, a + i, 0);
- T ("0123", a, a + i, 1);
- /* Offset in the range [0, i] is represented as a PHI (&a, &a + i)
- that the implementation isn't equipped to handle yet. */
- T ("0123", a, a + i, 2); /* { dg-warning "accessing 2 bytes at offsets 0 and \\\[0, 1] may overlap 1 byte at offset 1" "strncpy" { xfail *-*-* } } */
+ T ("0123", a, a + i, 1); /* { dg-warning "accessing 1 byte at offsets 0 and \\\[0, 1] may overlap 1 byte at offset 0" } */
+ /* When i == 1 the following overlaps at least 1 byte: the nul at a[1]
+ (if a + 1 is the empty string). If a + 1 is not empty then it overlaps
+ it plus as many non-nul characters after it, up to the total of 2. */
+ T ("0123", a, a + i, 2); /* { dg-warning "accessing 2 bytes at offsets 0 and \\\[0, 1] overlaps between 1 and 2 bytes at offset \\\[0, 1]" "strncpy" } */
i = SR (1, 5);
T ("0123", a, a + i, 0);
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-23.c b/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-23.c
index 112b08a..051c588 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-23.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-23.c
@@ -719,5 +719,5 @@ void test_overlap_with_precision (char *d, int i, int j)
T (d, "%.*s", i, d + 0); /* { dg-warning "may overlap" } */
T (d, "%.*s", i, d + 1); /* { dg-warning "may overlap" } */
T (d, "%.*s", i, d + 2);
- T (d, "%.*s", i, d + i); /* { dg-warning "may overlap" "" { xfail *-*-* } } */
+ T (d, "%.*s", i, d + i); /* { dg-warning "may overlap" } */
}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-1.c b/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-1.c
new file mode 100644
index 0000000..1d5b960
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-1.c
@@ -0,0 +1,36 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
+/* PR tree-optimization/109959 */
+
+unsigned fu(unsigned a)
+{
+ _Bool t = a <= 1;
+ return t & a;
+}
+
+_Bool fb(unsigned a)
+{
+ _Bool t = a <= 1;
+ return t & a;
+}
+
+_Bool fb1(unsigned a)
+{
+ _Bool t = a <= 1;
+ _Bool t1 = a;
+ return t & t1;
+}
+
+signed fui(unsigned a)
+{
+ _Bool t = a <= 1;
+ int ai = a;
+ return t & ai;
+}
+
+/* These all should be optimized to `a == 1` */
+/* { dg-final { scan-tree-dump-times "eq_expr," 4 "optimized"} } */
+/* { dg-final { scan-tree-dump-not "le_expr," "optimized"} } */
+/* { dg-final { scan-tree-dump-not "bit_and_expr," "optimized"} } */
+
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-2.c b/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-2.c
new file mode 100644
index 0000000..c727b9e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-2.c
@@ -0,0 +1,32 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
+/* PR tree-optimization/109959 */
+
+unsigned f(unsigned a)
+{
+ if (a <= 1)
+ return a;
+ return 0;
+}
+
+unsigned f0(unsigned a)
+{
+ if (a > 1)
+ return 0;
+ return a;
+}
+
+_Bool fb(unsigned a)
+{
+ if (a > 1)
+ return 0;
+ return a == 1;
+}
+
+/* These all should be optimized to `a == 1` */
+/* { dg-final { scan-tree-dump-times "eq_expr," 3 "optimized"} } */
+/* { dg-final { scan-tree-dump-not "le_expr," "optimized"} } */
+/* { dg-final { scan-tree-dump-not "bit_and," "optimized"} } */
+/* { dg-final { scan-tree-dump-not "gimple_phi " "optimized"} } */
+
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-3.c b/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-3.c
new file mode 100644
index 0000000..f2546b8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-3.c
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
+/* PR tree-optimization/109959 */
+
+
+_Bool f2(unsigned a, int t)
+{
+ void g(void);
+ if (t)
+ return 0;
+ g();
+ if (a > 1)
+ return 0;
+ return a == 1;
+}
+
+/* These all should be optimized to `a == 1` */
+/* { dg-final { scan-tree-dump-times "eq_expr," 1 "optimized"} } */
+/* { dg-final { scan-tree-dump-not "le_expr," "optimized"} } */
+/* { dg-final { scan-tree-dump-not "bit_and_expr," "optimized"} } */
+
+