aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2022-01-29 17:54:43 +0100
committerJakub Jelinek <jakub@redhat.com>2022-01-29 17:54:43 +0100
commita1544878966020d1f7a640b35d1f7a5f0e055624 (patch)
tree6578967d246f4f0b2147cb07cc2915b4772b552a /gcc
parentf6f2d6cfec1c2fe9570b98211be58329d8d7749b (diff)
downloadgcc-a1544878966020d1f7a640b35d1f7a5f0e055624.zip
gcc-a1544878966020d1f7a640b35d1f7a5f0e055624.tar.gz
gcc-a1544878966020d1f7a640b35d1f7a5f0e055624.tar.bz2
match.pd: Fix up 1 / X for unsigned X optimization [PR104280]
On Fri, Jan 28, 2022 at 11:38:23AM -0700, Jeff Law wrote: > Thanks.  Given the original submission and most of the review work was done > prior to stage3 closing, I went ahead and installed this on the trunk. Unfortunately this breaks quite a lot of things. The main problem is that GIMPLE allows EQ_EXPR etc. only with BOOLEAN_TYPE or with TYPE_PRECISION == 1 integral type (or vector boolean). Violating this causes verification failures in tree-cfg.cc in some cases, in other cases wrong-code issues because before it is verified we e.g. transform 1U / x into x == 1U and later into x (because we assume that == type must be one of the above cases and when it is the same type as the type of the first operand, for boolean-ish cases it should be equivalent). Fixed by changing that (eq @1 { build_one_cst (type); }) into (convert (eq:boolean_type_node @1 { build_one_cst (type); })) Note, I'm not 100% sure if :boolean_type_node is required in that case, I see some spots in match.pd that look exactly like this, while there is e.g. (convert (le ...)) that supposedly does the right thing too. The signed integer 1/X case doesn't need changes changes, for (cond (le ...) ...) le gets correctly boolean_type_node and cond should use type. I've also reformatted it, some lines were too long, match.pd uses indentation by 1 column instead of 2 etc. 2022-01-29 Jakub Jelinek <jakub@redhat.com> Andrew Pinski <apinski@marvell.com> PR tree-optimization/104279 PR tree-optimization/104280 PR tree-optimization/104281 * match.pd (1 / X -> X == 1 for unsigned X): Build eq with boolean_type_node and convert to type. Formatting fixes. * gcc.dg/torture/pr104279.c: New test. * gcc.dg/torture/pr104280.c: New test. * gcc.dg/torture/pr104281.c: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/match.pd28
-rw-r--r--gcc/testsuite/gcc.dg/torture/pr104279.c12
-rw-r--r--gcc/testsuite/gcc.dg/torture/pr104280.c16
-rw-r--r--gcc/testsuite/gcc.dg/torture/pr104281.c22
4 files changed, 66 insertions, 12 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index 0544ddd..b942cb2 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -435,18 +435,22 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
&& TYPE_UNSIGNED (type))
(trunc_divmod @0 @1))))
- /* 1 / X -> X == 1 for unsigned integer X.
- 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
- But not for 1 / 0 so that we can get proper warnings and errors,
- and not for 1-bit integers as they are edge cases better handled elsewhere. */
-(simplify
- (trunc_div integer_onep@0 @1)
- (if (INTEGRAL_TYPE_P (type) && !integer_zerop (@1) && TYPE_PRECISION (type) > 1)
- (if (TYPE_UNSIGNED (type))
- (eq @1 { build_one_cst (type); })
- (with { tree utype = unsigned_type_for (type); }
- (cond (le (plus (convert:utype @1) { build_one_cst (utype); }) { build_int_cst (utype, 2); })
- @1 { build_zero_cst (type); })))))
+/* 1 / X -> X == 1 for unsigned integer X.
+ 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
+ But not for 1 / 0 so that we can get proper warnings and errors,
+ and not for 1-bit integers as they are edge cases better handled
+ elsewhere. */
+(simplify
+ (trunc_div integer_onep@0 @1)
+ (if (INTEGRAL_TYPE_P (type)
+ && !integer_zerop (@1)
+ && TYPE_PRECISION (type) > 1)
+ (if (TYPE_UNSIGNED (type))
+ (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
+ (with { tree utype = unsigned_type_for (type); }
+ (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
+ { build_int_cst (utype, 2); })
+ @1 { build_zero_cst (type); })))))
/* Combine two successive divisions. Note that combining ceil_div
and floor_div is trickier and combining round_div even more so. */
diff --git a/gcc/testsuite/gcc.dg/torture/pr104279.c b/gcc/testsuite/gcc.dg/torture/pr104279.c
new file mode 100644
index 0000000..7c4fc20
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr104279.c
@@ -0,0 +1,12 @@
+/* PR tree-optimization/104279 */
+/* { dg-do compile } */
+
+unsigned a, b;
+
+int
+main ()
+{
+ b = ~(0 || ~0);
+ a = ~b / ~a;
+ return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr104280.c b/gcc/testsuite/gcc.dg/torture/pr104280.c
new file mode 100644
index 0000000..c5a1d9b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr104280.c
@@ -0,0 +1,16 @@
+/* PR tree-optimization/104280 */
+/* { dg-do run } */
+
+int
+foo (unsigned b, int c)
+{
+ return b / c;
+}
+
+int
+main ()
+{
+ if (foo (1, 2) != 0)
+ __builtin_abort ();
+ return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr104281.c b/gcc/testsuite/gcc.dg/torture/pr104281.c
new file mode 100644
index 0000000..35c8e20
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr104281.c
@@ -0,0 +1,22 @@
+/* PR tree-optimization/104281 */
+/* { dg-do run } */
+
+unsigned a = 1;
+int b, c = 2;
+long d;
+
+int
+main ()
+{
+ while (1)
+ {
+ int m = a;
+ L:
+ a = ~(-(m || b & d));
+ b = ((1 ^ a) / c);
+ if (b)
+ goto L;
+ break;
+ }
+ return 0;
+}