aboutsummaryrefslogtreecommitdiff
path: root/gcc/match.pd
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2023-12-15 10:13:44 +0100
committerJakub Jelinek <jakub@redhat.com>2023-12-15 10:13:44 +0100
commit330f1e078dae257d4f8f7b13299ececc2344e681 (patch)
treee9c88b7bf3f9ec05d41629ea8c837f7ce2bafdfc /gcc/match.pd
parentd50d3d0a688e8dac31b307b3aad3fbc99283ebc4 (diff)
downloadgcc-330f1e078dae257d4f8f7b13299ececc2344e681.zip
gcc-330f1e078dae257d4f8f7b13299ececc2344e681.tar.gz
gcc-330f1e078dae257d4f8f7b13299ececc2344e681.tar.bz2
match.pd: Optimize sign-extension followed by truncation [PR113024]
While looking at a bitint ICE, I've noticed we don't optimize in f1 and f5 functions below the 2 casts into just one at GIMPLE, even when optimize it in convert_to_integer if it appears in the same stmt. The large match.pd simplification of two conversions in a row has many complex rules and as the testcase shows, everything else from the narrowest -> widest -> prec_in_between all integer conversions is already handled, either because the inside_unsignedp == inter_unsignedp rule kicks in, or the && ((inter_unsignedp && inter_prec > inside_prec) == (final_unsignedp && final_prec > inter_prec)) one, but there is no reason why sign extension to from narrowest to widest type followed by truncation to something in between can't be done just as sign extension from narrowest to the final type. After all, if the widest type is signed rather than unsigned, regardless of the final type signedness we already handle it that way. And since PR93044 we also handle it if the final precision is not wider than the inside precision. 2023-12-15 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/113024 * match.pd (two conversions in a row): Simplify scalar integer sign-extension followed by truncation. * gcc.dg/tree-ssa/pr113024.c: New test.
Diffstat (limited to 'gcc/match.pd')
-rw-r--r--gcc/match.pd7
1 files changed, 5 insertions, 2 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index 562880a..d57e29b 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4754,11 +4754,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
/* If we have a sign-extension of a zero-extended value, we can
replace that by a single zero-extension. Likewise if the
final conversion does not change precision we can drop the
- intermediate conversion. */
+ intermediate conversion. Similarly truncation of a sign-extension
+ can be replaced by a single sign-extension. */
(if (inside_int && inter_int && final_int
&& ((inside_prec < inter_prec && inter_prec < final_prec
&& inside_unsignedp && !inter_unsignedp)
- || final_prec == inter_prec))
+ || final_prec == inter_prec
+ || (inside_prec < inter_prec && inter_prec > final_prec
+ && !inside_unsignedp && inter_unsignedp)))
(ocvt @0))
/* Two conversions in a row are not needed unless: