]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Test: Add test case for bit_op convert folding
authorPan Li <pan2.li@intel.com>
Sat, 15 Nov 2025 03:21:37 +0000 (11:21 +0800)
committerPan Li <pan2.li@intel.com>
Sun, 23 Nov 2025 10:36:39 +0000 (18:36 +0800)
Add test cases of all possible types of bit_op convert folding.
To check there is no tree dump like below:

   _5 = (uint8_t) _2;
   return _5;

gcc/testsuite/ChangeLog:

* gcc.dg/tree-ssa/bit_op_cvt.1.c: New test.
* gcc.dg/tree-ssa/bit_op_cvt.2.c: New test.
* gcc.dg/tree-ssa/bit_op_cvt.3.c: New test.
* gcc.dg/tree-ssa/bit_op_cvt.4.c: New test.
* gcc.dg/tree-ssa/bit_op_cvt.5.c: New test.
* gcc.dg/tree-ssa/bit_op_cvt.6.c: New test.
* gcc.dg/tree-ssa/bit_op_cvt.h: New test.

Signed-off-by: Pan Li <pan2.li@intel.com>
gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.4.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.5.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.6.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.h [new file with mode: 0644]

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.1.c b/gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.1.c
new file mode 100644 (file)
index 0000000..c0da601
--- /dev/null
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+#include "bit_op_cvt.h"
+
+DEF_BIT_CVT_0(uint8_t, int8_t, uint16_t, |, bit_ior)
+DEF_BIT_CVT_0(uint8_t, int8_t, uint32_t, |, bit_ior)
+DEF_BIT_CVT_0(uint8_t, int8_t, uint64_t, |, bit_ior)
+DEF_BIT_CVT_0(uint8_t, uint8_t, uint16_t, |, bit_ior)
+DEF_BIT_CVT_0(uint8_t, uint8_t, uint32_t, |, bit_ior)
+DEF_BIT_CVT_0(uint8_t, uint8_t, uint64_t, |, bit_ior)
+DEF_BIT_CVT_0(uint8_t, uint8_t, int16_t, |, bit_ior)
+DEF_BIT_CVT_0(uint8_t, uint8_t, int32_t, |, bit_ior)
+DEF_BIT_CVT_0(uint8_t, uint8_t, int64_t, |, bit_ior)
+DEF_BIT_CVT_0(uint8_t, int8_t, int16_t, |, bit_ior)
+DEF_BIT_CVT_0(uint8_t, int8_t, int32_t, |, bit_ior)
+DEF_BIT_CVT_0(uint8_t, int8_t, int64_t, |, bit_ior)
+
+DEF_BIT_CVT_0(uint16_t, int16_t, uint32_t, |, bit_ior)
+DEF_BIT_CVT_0(uint16_t, int16_t, uint64_t, |, bit_ior)
+DEF_BIT_CVT_0(uint16_t, int16_t, int32_t, |, bit_ior)
+DEF_BIT_CVT_0(uint16_t, int16_t, int64_t, |, bit_ior)
+DEF_BIT_CVT_0(uint16_t, uint16_t, uint32_t, |, bit_ior)
+DEF_BIT_CVT_0(uint16_t, uint16_t, uint64_t, |, bit_ior)
+DEF_BIT_CVT_0(uint16_t, uint16_t, int32_t, |, bit_ior)
+DEF_BIT_CVT_0(uint16_t, uint16_t, int64_t, |, bit_ior)
+
+DEF_BIT_CVT_0(uint32_t, int32_t, uint64_t, |, bit_ior)
+DEF_BIT_CVT_0(uint32_t, int32_t, int64_t, |, bit_ior)
+DEF_BIT_CVT_0(uint32_t, uint32_t, uint64_t, |, bit_ior)
+DEF_BIT_CVT_0(uint32_t, uint32_t, int64_t, |, bit_ior)
+
+/* { dg-final { scan-tree-dump-not "_\[0-9]\+ = \\(u?int\[0-9]\+_t\\) _\[0-9]\+;\\s+return _\[0-9]\+;" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.2.c b/gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.2.c
new file mode 100644 (file)
index 0000000..38671d2
--- /dev/null
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+#include "bit_op_cvt.h"
+
+DEF_BIT_CVT_0(int8_t, int8_t, uint16_t, |, bit_ior)
+DEF_BIT_CVT_0(int8_t, int8_t, uint32_t, |, bit_ior)
+DEF_BIT_CVT_0(int8_t, int8_t, uint64_t, |, bit_ior)
+DEF_BIT_CVT_0(int8_t, uint8_t, uint16_t, |, bit_ior)
+DEF_BIT_CVT_0(int8_t, uint8_t, uint32_t, |, bit_ior)
+DEF_BIT_CVT_0(int8_t, uint8_t, uint64_t, |, bit_ior)
+DEF_BIT_CVT_0(int8_t, uint8_t, int16_t, |, bit_ior)
+DEF_BIT_CVT_0(int8_t, uint8_t, int32_t, |, bit_ior)
+DEF_BIT_CVT_0(int8_t, uint8_t, int64_t, |, bit_ior)
+DEF_BIT_CVT_0(int8_t, int8_t, int16_t, |, bit_ior)
+DEF_BIT_CVT_0(int8_t, int8_t, int32_t, |, bit_ior)
+DEF_BIT_CVT_0(int8_t, int8_t, int64_t, |, bit_ior)
+
+DEF_BIT_CVT_0(int16_t, int16_t, uint32_t, |, bit_ior)
+DEF_BIT_CVT_0(int16_t, int16_t, uint64_t, |, bit_ior)
+DEF_BIT_CVT_0(int16_t, int16_t, int32_t, |, bit_ior)
+DEF_BIT_CVT_0(int16_t, int16_t, int64_t, |, bit_ior)
+DEF_BIT_CVT_0(int16_t, uint16_t, uint32_t, |, bit_ior)
+DEF_BIT_CVT_0(int16_t, uint16_t, uint64_t, |, bit_ior)
+DEF_BIT_CVT_0(int16_t, uint16_t, int32_t, |, bit_ior)
+DEF_BIT_CVT_0(int16_t, uint16_t, int64_t, |, bit_ior)
+
+DEF_BIT_CVT_0(int32_t, int32_t, uint64_t, |, bit_ior)
+DEF_BIT_CVT_0(int32_t, int32_t, int64_t, |, bit_ior)
+DEF_BIT_CVT_0(int32_t, uint32_t, uint64_t, |, bit_ior)
+DEF_BIT_CVT_0(int32_t, uint32_t, int64_t, |, bit_ior)
+
+/* { dg-final { scan-tree-dump-not "_\[0-9]\+ = \\(u?int\[0-9]\+_t\\) _\[0-9]\+;\\s+return _\[0-9]\+;" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.3.c b/gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.3.c
new file mode 100644 (file)
index 0000000..8439345
--- /dev/null
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+#include "bit_op_cvt.h"
+
+DEF_BIT_CVT_0(uint8_t, int8_t, uint16_t, &, bit_and)
+DEF_BIT_CVT_0(uint8_t, int8_t, uint32_t, &, bit_and)
+DEF_BIT_CVT_0(uint8_t, int8_t, uint64_t, &, bit_and)
+DEF_BIT_CVT_0(uint8_t, uint8_t, uint16_t, &, bit_and)
+DEF_BIT_CVT_0(uint8_t, uint8_t, uint32_t, &, bit_and)
+DEF_BIT_CVT_0(uint8_t, uint8_t, uint64_t, &, bit_and)
+DEF_BIT_CVT_0(uint8_t, uint8_t, int16_t, &, bit_and)
+DEF_BIT_CVT_0(uint8_t, uint8_t, int32_t, &, bit_and)
+DEF_BIT_CVT_0(uint8_t, uint8_t, int64_t, &, bit_and)
+DEF_BIT_CVT_0(uint8_t, int8_t, int16_t, &, bit_and)
+DEF_BIT_CVT_0(uint8_t, int8_t, int32_t, &, bit_and)
+DEF_BIT_CVT_0(uint8_t, int8_t, int64_t, &, bit_and)
+
+DEF_BIT_CVT_0(uint16_t, int16_t, uint32_t, &, bit_and)
+DEF_BIT_CVT_0(uint16_t, int16_t, uint64_t, &, bit_and)
+DEF_BIT_CVT_0(uint16_t, int16_t, int32_t, &, bit_and)
+DEF_BIT_CVT_0(uint16_t, int16_t, int64_t, &, bit_and)
+DEF_BIT_CVT_0(uint16_t, uint16_t, uint32_t, &, bit_and)
+DEF_BIT_CVT_0(uint16_t, uint16_t, uint64_t, &, bit_and)
+DEF_BIT_CVT_0(uint16_t, uint16_t, int32_t, &, bit_and)
+DEF_BIT_CVT_0(uint16_t, uint16_t, int64_t, &, bit_and)
+
+DEF_BIT_CVT_0(uint32_t, int32_t, uint64_t, &, bit_and)
+DEF_BIT_CVT_0(uint32_t, int32_t, int64_t, &, bit_and)
+DEF_BIT_CVT_0(uint32_t, uint32_t, uint64_t, &, bit_and)
+DEF_BIT_CVT_0(uint32_t, uint32_t, int64_t, &, bit_and)
+
+/* { dg-final { scan-tree-dump-not "_\[0-9]\+ = \\(u?int\[0-9]\+_t\\) _\[0-9]\+;\\s+return _\[0-9]\+;" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.4.c b/gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.4.c
new file mode 100644 (file)
index 0000000..3f3356b
--- /dev/null
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+#include "bit_op_cvt.h"
+
+DEF_BIT_CVT_0(int8_t, int8_t, uint16_t, &, bit_and)
+DEF_BIT_CVT_0(int8_t, int8_t, uint32_t, &, bit_and)
+DEF_BIT_CVT_0(int8_t, int8_t, uint64_t, &, bit_and)
+DEF_BIT_CVT_0(int8_t, uint8_t, uint16_t, &, bit_and)
+DEF_BIT_CVT_0(int8_t, uint8_t, uint32_t, &, bit_and)
+DEF_BIT_CVT_0(int8_t, uint8_t, uint64_t, &, bit_and)
+DEF_BIT_CVT_0(int8_t, uint8_t, int16_t, &, bit_and)
+DEF_BIT_CVT_0(int8_t, uint8_t, int32_t, &, bit_and)
+DEF_BIT_CVT_0(int8_t, uint8_t, int64_t, &, bit_and)
+DEF_BIT_CVT_0(int8_t, int8_t, int16_t, &, bit_and)
+DEF_BIT_CVT_0(int8_t, int8_t, int32_t, &, bit_and)
+DEF_BIT_CVT_0(int8_t, int8_t, int64_t, &, bit_and)
+
+DEF_BIT_CVT_0(int16_t, int16_t, uint32_t, &, bit_and)
+DEF_BIT_CVT_0(int16_t, int16_t, uint64_t, &, bit_and)
+DEF_BIT_CVT_0(int16_t, int16_t, int32_t, &, bit_and)
+DEF_BIT_CVT_0(int16_t, int16_t, int64_t, &, bit_and)
+DEF_BIT_CVT_0(int16_t, uint16_t, uint32_t, &, bit_and)
+DEF_BIT_CVT_0(int16_t, uint16_t, uint64_t, &, bit_and)
+DEF_BIT_CVT_0(int16_t, uint16_t, int32_t, &, bit_and)
+DEF_BIT_CVT_0(int16_t, uint16_t, int64_t, &, bit_and)
+
+DEF_BIT_CVT_0(int32_t, int32_t, uint64_t, &, bit_and)
+DEF_BIT_CVT_0(int32_t, int32_t, int64_t, &, bit_and)
+DEF_BIT_CVT_0(int32_t, uint32_t, uint64_t, &, bit_and)
+DEF_BIT_CVT_0(int32_t, uint32_t, int64_t, &, bit_and)
+
+/* { dg-final { scan-tree-dump-not "_\[0-9]\+ = \\(u?int\[0-9]\+_t\\) _\[0-9]\+;\\s+return _\[0-9]\+;" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.5.c b/gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.5.c
new file mode 100644 (file)
index 0000000..e746d6e
--- /dev/null
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+#include "bit_op_cvt.h"
+
+DEF_BIT_CVT_0(uint8_t, int8_t, uint16_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint8_t, int8_t, uint32_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint8_t, int8_t, uint64_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint8_t, uint8_t, uint16_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint8_t, uint8_t, uint32_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint8_t, uint8_t, uint64_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint8_t, uint8_t, int16_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint8_t, uint8_t, int32_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint8_t, uint8_t, int64_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint8_t, int8_t, int16_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint8_t, int8_t, int32_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint8_t, int8_t, int64_t, ^, bit_xor)
+
+DEF_BIT_CVT_0(uint16_t, int16_t, uint32_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint16_t, int16_t, uint64_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint16_t, int16_t, int32_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint16_t, int16_t, int64_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint16_t, uint16_t, uint32_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint16_t, uint16_t, uint64_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint16_t, uint16_t, int32_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint16_t, uint16_t, int64_t, ^, bit_xor)
+
+DEF_BIT_CVT_0(uint32_t, int32_t, uint64_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint32_t, int32_t, int64_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint32_t, uint32_t, uint64_t, ^, bit_xor)
+DEF_BIT_CVT_0(uint32_t, uint32_t, int64_t, ^, bit_xor)
+
+/* { dg-final { scan-tree-dump-not "_\[0-9]\+ = \\(u?int\[0-9]\+_t\\) _\[0-9]\+;\\s+return _\[0-9]\+;" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.6.c b/gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.6.c
new file mode 100644 (file)
index 0000000..a12dc88
--- /dev/null
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+#include "bit_op_cvt.h"
+
+DEF_BIT_CVT_0(int8_t, int8_t, uint16_t, ^, bit_xor)
+DEF_BIT_CVT_0(int8_t, int8_t, uint32_t, ^, bit_xor)
+DEF_BIT_CVT_0(int8_t, int8_t, uint64_t, ^, bit_xor)
+DEF_BIT_CVT_0(int8_t, uint8_t, uint16_t, ^, bit_xor)
+DEF_BIT_CVT_0(int8_t, uint8_t, uint32_t, ^, bit_xor)
+DEF_BIT_CVT_0(int8_t, uint8_t, uint64_t, ^, bit_xor)
+DEF_BIT_CVT_0(int8_t, uint8_t, int16_t, ^, bit_xor)
+DEF_BIT_CVT_0(int8_t, uint8_t, int32_t, ^, bit_xor)
+DEF_BIT_CVT_0(int8_t, uint8_t, int64_t, ^, bit_xor)
+DEF_BIT_CVT_0(int8_t, int8_t, int16_t, ^, bit_xor)
+DEF_BIT_CVT_0(int8_t, int8_t, int32_t, ^, bit_xor)
+DEF_BIT_CVT_0(int8_t, int8_t, int64_t, ^, bit_xor)
+
+DEF_BIT_CVT_0(int16_t, int16_t, uint32_t, ^, bit_xor)
+DEF_BIT_CVT_0(int16_t, int16_t, uint64_t, ^, bit_xor)
+DEF_BIT_CVT_0(int16_t, int16_t, int32_t, ^, bit_xor)
+DEF_BIT_CVT_0(int16_t, int16_t, int64_t, ^, bit_xor)
+DEF_BIT_CVT_0(int16_t, uint16_t, uint32_t, ^, bit_xor)
+DEF_BIT_CVT_0(int16_t, uint16_t, uint64_t, ^, bit_xor)
+DEF_BIT_CVT_0(int16_t, uint16_t, int32_t, ^, bit_xor)
+DEF_BIT_CVT_0(int16_t, uint16_t, int64_t, ^, bit_xor)
+
+DEF_BIT_CVT_0(int32_t, int32_t, uint64_t, ^, bit_xor)
+DEF_BIT_CVT_0(int32_t, int32_t, int64_t, ^, bit_xor)
+DEF_BIT_CVT_0(int32_t, uint32_t, uint64_t, ^, bit_xor)
+DEF_BIT_CVT_0(int32_t, uint32_t, int64_t, ^, bit_xor)
+
+/* { dg-final { scan-tree-dump-not "_\[0-9]\+ = \\(u?int\[0-9]\+_t\\) _\[0-9]\+;\\s+return _\[0-9]\+;" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.h b/gcc/testsuite/gcc.dg/tree-ssa/bit_op_cvt.h
new file mode 100644 (file)
index 0000000..87d67e6
--- /dev/null
@@ -0,0 +1,13 @@
+#ifndef HAVE_DEFINED_BIT_OP_CVT
+#define HAVE_DEFINED_BIT_OP_CVT
+
+#include <stdint.h>
+
+#define DEF_BIT_CVT_0(T1, T2, T3, OP, NAME) \
+T1 test_bit_##NAME##_##T1##_##T2##_##T3##_0(T2 a, \
+                                           T3 b) \
+{ \
+  return (T1)(a OP (T3)b); \
+}
+
+#endif