]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
MATCH: Simplify zero/sign extension bit operations [PR122848]
authorEikansh Gupta <eikansh.gupta@oss.qualcomm.com>
Wed, 3 Jun 2026 10:16:29 +0000 (15:46 +0530)
committerEikansh Gupta <eikansh.gupta@oss.qualcomm.com>
Thu, 30 Jul 2026 10:44:28 +0000 (16:14 +0530)
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
gcc/testsuite/gcc.dg/tree-ssa/pr122848.c [new file with mode: 0644]

index 6c9bb0da7cf0cae6ff024d3a8298691e5d23ce01..ce02ebf26997b0e319fde1a8b3f40f29b54491d7 100644 (file)
@@ -1488,6 +1488,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))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr122848.c b/gcc/testsuite/gcc.dg/tree-ssa/pr122848.c
new file mode 100644 (file)
index 0000000..363e000
--- /dev/null
@@ -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" } } */