MATCH: Simplify zero/sign extension bit operations [PR122848]
Checks
Commit Message
Fold bitwise operations involving zero and sign extensions from the same
low-precision value. The AND case folds to the zero extension, and the
OR case folds to the sign extension.
PR tree-optimization/122848
gcc/ChangeLog:
* match.pd (zero_extend & sign_extend -> zero_extend): New pattern.
(zero_extend | sign_extend -> sign_extend): Likewise.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/pr122848.c: New test.
Signed-off-by: Eikansh Gupta <eikansh.gupta@oss.qualcomm.com>
---
gcc/match.pd | 15 +++++
gcc/testsuite/gcc.dg/tree-ssa/pr122848.c | 70 ++++++++++++++++++++++++
2 files changed, 85 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr122848.c
Comments
On 7/7/2026 5:19 AM, Eikansh Gupta wrote:
> Fold bitwise operations involving zero and sign extensions from the same
> low-precision value. The AND case folds to the zero extension, and the
> OR case folds to the sign extension.
>
> PR tree-optimization/122848
>
> gcc/ChangeLog:
>
> * match.pd (zero_extend & sign_extend -> zero_extend): New pattern.
> (zero_extend | sign_extend -> sign_extend): Likewise.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.dg/tree-ssa/pr122848.c: New test.
LGTM. I had to convince myself the nop_conversion was needed, but it
(like the convert nodes) is inherently needed for what you're trying to do.
Given match.pd isn't a strong area for me, I'd give others another 48hrs
to chime in before committing. Which raises the issue of commit privs.
I'm pretty sure that Andrea and I have been pushing patches for you. We
should probably go ahead and get you commit privs so you can push
approved patches.
This page as a link to a form you fill out:
https://gcc.gnu.org/gitwrite.html
List me as approving your request. You can use any email for me you
want :-) The admins will likely recognize jeffreyalaw@gmail.com
easiest, but again, any will work. Once approved you'll create an entry
in the MAINTAINERS file and push this patch if there aren't any change
requests/objections in the next 48hrs.
Jeff
>
> Given match.pd isn't a strong area for me, I'd give others another 48hrs
> to chime in before committing. Which raises the issue of commit privs.
> I'm pretty sure that Andrea and I have been pushing patches for you. We
> should probably go ahead and get you commit privs so you can push
> approved patches.
>
> This page as a link to a form you fill out:
>
>
> https://gcc.gnu.org/gitwrite.html
>
> List me as approving your request. You can use any email for me you
> want :-) The admins will likely recognize jeffreyalaw@gmail.com
> easiest, but again, any will work. Once approved you'll create an entry
> in the MAINTAINERS file and push this patch if there aren't any change
> requests/objections in the next 48hrs.
>
Thanks Jeff. I have filled out the form.
Regards
Eikansh
@@ -1472,6 +1472,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
&& bitwise_inverted_equal_p (@0, @1, wascmp))
{ wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
+/* zero_extend (X) & sign_extend (X) -> zero_extend (X) and
+ zero_extend (X) | sign_extend (X) -> sign_extend (X) */
+(for bitop (bit_and bit_ior)
+ (simplify
+ (bitop:c (convert @0) (convert (nop_convert@1 @0)))
+ (if (INTEGRAL_TYPE_P (type)
+ && INTEGRAL_TYPE_P (TREE_TYPE (@0))
+ && INTEGRAL_TYPE_P (TREE_TYPE (@1))
+ && TYPE_UNSIGNED (TREE_TYPE (@0)) != TYPE_UNSIGNED (TREE_TYPE (@1))
+ && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
+ && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type))
+ (if ((bitop == BIT_AND_EXPR) == TYPE_UNSIGNED (TREE_TYPE (@0)))
+ (convert @0)
+ (convert @1)))))
+
/* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
(simplify
(bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
new file mode 100644
@@ -0,0 +1,70 @@
+/* PR tree-optimization/122848 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int f1 (unsigned char a)
+{
+ int t = a;
+ int t1 = (signed char) a;
+ return t & t1;
+}
+
+int f2 (unsigned char a)
+{
+ int t = a;
+ int t1 = (signed char) a;
+ return t1 & t;
+}
+
+int f3 (unsigned char a)
+{
+ return (int) a & (int) (signed char) a;
+}
+
+int f4 (unsigned short a)
+{
+ return (int) a & (int) (short) a;
+}
+
+long f5 (unsigned int a)
+{
+ return (long) a & (long) (int) a;
+}
+
+unsigned int f6 (unsigned char a)
+{
+ return (unsigned int) a & (unsigned int) (signed char) a;
+}
+
+int f7 (unsigned char a)
+{
+ return (int) a | (int) (signed char) a;
+}
+
+int f8 (unsigned short a)
+{
+ return (int) a | (int) (short) a;
+}
+
+int f9 (signed char a)
+{
+ return (int) (unsigned char) a & (int) a;
+}
+
+int f10 (signed char a)
+{
+ return (int) a & (int) (unsigned char) a;
+}
+
+int f11 (signed char a)
+{
+ return (int) (unsigned char) a | (int) a;
+}
+
+int f12 (signed short a)
+{
+ return (int) (unsigned short) a | (int) a;
+}
+
+/* { dg-final { scan-tree-dump-not " & " "optimized" } } */
+/* { dg-final { scan-tree-dump-not " \\| " "optimized" } } */