The C and C++ frontends were incorrectly generating RDIV_EXPR instead of
TRUNC_DIV_EXPR for division of vectors with enum element types.

The issue was that the division operator handling only checked for
INTEGER_TYPE (and BITINT_TYPE in the C frontend) when determining whether
to use integer division. However, ENUMERAL_TYPE is also represents an
integral type. When enum types were used in vectors, the frontend would
incorrectly fall through to RDIV_EXPR, causing an ICE during the
veclower2 pass because RDIV_EXPR is only valid for floating-point types.

The fix adds ENUMERAL_TYPE to the type checks. Note that scalar enum
operands are promoted to int before reaching this code, but enum
*vectors* are not promoted.

gcc/c/ChangeLog:

        PR c/123437
        * c-typeck.cc (build_binary_op): Include ENUMERAL_TYPE in
        integer division check for vectors.

gcc/cp/ChangeLog:

        PR c/123437
        * typeck.cc (cp_build_binary_op): Include ENUMERAL_TYPE in
        integer division check.

gcc/testsuite/ChangeLog:

        PR c/123437
        * c-c++-common/pr123437.c: New test.
---
 gcc/c/c-typeck.cc                     |  6 ++++--
 gcc/cp/typeck.cc                      |  3 ++-
 gcc/testsuite/c-c++-common/pr123437.c | 11 +++++++++++
 3 files changed, 17 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/c-c++-common/pr123437.c

diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index 1903285b626..f08e051ad4e 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -14478,8 +14478,10 @@ build_binary_op (location_t location, enum tree_code 
code,
          if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
            tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
 
-         if (!(((tcode0 == INTEGER_TYPE || tcode0 == BITINT_TYPE)
-                && (tcode1 == INTEGER_TYPE || tcode1 == BITINT_TYPE))
+         if (!(((tcode0 == INTEGER_TYPE || tcode0 == BITINT_TYPE
+                 || tcode0 == ENUMERAL_TYPE)
+                && (tcode1 == INTEGER_TYPE || tcode1 == BITINT_TYPE
+                    || tcode1 == ENUMERAL_TYPE))
                || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
            resultcode = RDIV_EXPR;
          else
diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
index f6f71a03003..347ce7de1e0 100644
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -5856,7 +5856,8 @@ cp_build_binary_op (const op_location_t &location,
          if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
            tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
 
-         if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
+         if (!((tcode0 == INTEGER_TYPE || tcode0 == ENUMERAL_TYPE)
+               && (tcode1 == INTEGER_TYPE || tcode1 == ENUMERAL_TYPE)))
            resultcode = RDIV_EXPR;
          else
            {
diff --git a/gcc/testsuite/c-c++-common/pr123437.c 
b/gcc/testsuite/c-c++-common/pr123437.c
new file mode 100644
index 00000000000..78e48a28700
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/pr123437.c
@@ -0,0 +1,11 @@
+/* PR c/123437 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+enum foo { F };
+typedef enum foo vec_foo __attribute__((vector_size(16)));
+
+vec_foo div(vec_foo x, vec_foo y)
+{
+  return x / y;
+}
-- 
2.52.0

Reply via email to