* [PATCH] reassoc: Fix up optimize_range_tests_to_bit_test [PR114965]
@ 2024-05-08 7:56 Jakub Jelinek
2024-05-08 8:12 ` Richard Biener
0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2024-05-08 7:56 UTC (permalink / raw)
To: Richard Biener; +Cc: gcc-patches
Hi!
The optimize_range_tests_to_bit_test optimization normally emits a range
test first:
if (entry_test_needed)
{
tem = build_range_check (loc, optype, unshare_expr (exp),
false, lowi, high);
if (tem == NULL_TREE || is_gimple_val (tem))
continue;
}
so during the bit test we already know that exp is in the [lowi, high]
range, but skips it if we have range info which tells us this isn't
necessary.
Also, normally it emits shifts by exp - lowi counter, but has an
optimization to use just exp counter if the mask isn't a more expensive
constant in that case and lowi is > 0 and high is smaller than prec.
The following testcase is miscompiled because the two abnormal cases
are triggered. The range of exp is [43, 43][48, 48][95, 95], so we on
64-bit arch decide we don't need the entry test, because 95 - 43 < 64.
And we also decide to use just exp as counter, because the range test
tests just for exp == 43 || exp == 48, so high is smaller than 64 too.
Because 95 is in the exp range, we can't do that, we'd either need to
do a range test first, i.e.
if (exp - 43U <= 48U - 43U) if ((1UL << exp) & mask1))
or need to subtract lowi from the shift counter, i.e.
if ((1UL << (exp - 43)) & mask2)
but can't do both unless r.upper_bound () is < prec.
The following patch ensures that.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
2024-05-08 Jakub Jelinek <jakub@redhat.com>
PR tree-optimization/114965
* tree-ssa-reassoc.cc (optimize_range_tests_to_bit_test): Don't try to
optimize away exp - lowi subtraction from shift count unless entry
test is emitted or unless r.upper_bound () is smaller than prec.
* gcc.c-torture/execute/pr114965.c: New test.
--- gcc/tree-ssa-reassoc.cc.jj 2024-01-12 10:07:58.384848977 +0100
+++ gcc/tree-ssa-reassoc.cc 2024-05-07 18:18:45.558814991 +0200
@@ -3418,7 +3418,8 @@ optimize_range_tests_to_bit_test (enum t
We can avoid then subtraction of the minimum value, but the
mask constant could be perhaps more expensive. */
if (compare_tree_int (lowi, 0) > 0
- && compare_tree_int (high, prec) < 0)
+ && compare_tree_int (high, prec) < 0
+ && (entry_test_needed || wi::ltu_p (r.upper_bound (), prec)))
{
int cost_diff;
HOST_WIDE_INT m = tree_to_uhwi (lowi);
--- gcc/testsuite/gcc.c-torture/execute/pr114965.c.jj 2024-05-07 18:17:16.767031821 +0200
+++ gcc/testsuite/gcc.c-torture/execute/pr114965.c 2024-05-07 18:15:52.332188943 +0200
@@ -0,0 +1,30 @@
+/* PR tree-optimization/114965 */
+
+static void
+foo (const char *x)
+{
+
+ char a = '0';
+ while (1)
+ {
+ switch (*x)
+ {
+ case '_':
+ case '+':
+ a = *x;
+ x++;
+ continue;
+ default:
+ break;
+ }
+ break;
+ }
+ if (a == '0' || a == '+')
+ __builtin_abort ();
+}
+
+int
+main ()
+{
+ foo ("_");
+}
Jakub
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] reassoc: Fix up optimize_range_tests_to_bit_test [PR114965]
2024-05-08 7:56 [PATCH] reassoc: Fix up optimize_range_tests_to_bit_test [PR114965] Jakub Jelinek
@ 2024-05-08 8:12 ` Richard Biener
0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2024-05-08 8:12 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: gcc-patches
On Wed, 8 May 2024, Jakub Jelinek wrote:
> Hi!
>
> The optimize_range_tests_to_bit_test optimization normally emits a range
> test first:
> if (entry_test_needed)
> {
> tem = build_range_check (loc, optype, unshare_expr (exp),
> false, lowi, high);
> if (tem == NULL_TREE || is_gimple_val (tem))
> continue;
> }
> so during the bit test we already know that exp is in the [lowi, high]
> range, but skips it if we have range info which tells us this isn't
> necessary.
> Also, normally it emits shifts by exp - lowi counter, but has an
> optimization to use just exp counter if the mask isn't a more expensive
> constant in that case and lowi is > 0 and high is smaller than prec.
>
> The following testcase is miscompiled because the two abnormal cases
> are triggered. The range of exp is [43, 43][48, 48][95, 95], so we on
> 64-bit arch decide we don't need the entry test, because 95 - 43 < 64.
> And we also decide to use just exp as counter, because the range test
> tests just for exp == 43 || exp == 48, so high is smaller than 64 too.
> Because 95 is in the exp range, we can't do that, we'd either need to
> do a range test first, i.e.
> if (exp - 43U <= 48U - 43U) if ((1UL << exp) & mask1))
> or need to subtract lowi from the shift counter, i.e.
> if ((1UL << (exp - 43)) & mask2)
> but can't do both unless r.upper_bound () is < prec.
>
> The following patch ensures that.
>
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
OK.
Thanks,
Richard.
> 2024-05-08 Jakub Jelinek <jakub@redhat.com>
>
> PR tree-optimization/114965
> * tree-ssa-reassoc.cc (optimize_range_tests_to_bit_test): Don't try to
> optimize away exp - lowi subtraction from shift count unless entry
> test is emitted or unless r.upper_bound () is smaller than prec.
>
> * gcc.c-torture/execute/pr114965.c: New test.
>
> --- gcc/tree-ssa-reassoc.cc.jj 2024-01-12 10:07:58.384848977 +0100
> +++ gcc/tree-ssa-reassoc.cc 2024-05-07 18:18:45.558814991 +0200
> @@ -3418,7 +3418,8 @@ optimize_range_tests_to_bit_test (enum t
> We can avoid then subtraction of the minimum value, but the
> mask constant could be perhaps more expensive. */
> if (compare_tree_int (lowi, 0) > 0
> - && compare_tree_int (high, prec) < 0)
> + && compare_tree_int (high, prec) < 0
> + && (entry_test_needed || wi::ltu_p (r.upper_bound (), prec)))
> {
> int cost_diff;
> HOST_WIDE_INT m = tree_to_uhwi (lowi);
> --- gcc/testsuite/gcc.c-torture/execute/pr114965.c.jj 2024-05-07 18:17:16.767031821 +0200
> +++ gcc/testsuite/gcc.c-torture/execute/pr114965.c 2024-05-07 18:15:52.332188943 +0200
> @@ -0,0 +1,30 @@
> +/* PR tree-optimization/114965 */
> +
> +static void
> +foo (const char *x)
> +{
> +
> + char a = '0';
> + while (1)
> + {
> + switch (*x)
> + {
> + case '_':
> + case '+':
> + a = *x;
> + x++;
> + continue;
> + default:
> + break;
> + }
> + break;
> + }
> + if (a == '0' || a == '+')
> + __builtin_abort ();
> +}
> +
> +int
> +main ()
> +{
> + foo ("_");
> +}
>
> Jakub
>
>
--
Richard Biener <rguenther@suse.de>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-05-08 8:12 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-08 7:56 [PATCH] reassoc: Fix up optimize_range_tests_to_bit_test [PR114965] Jakub Jelinek
2024-05-08 8:12 ` Richard Biener
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).