PR c++/93238 - short right-shift with enum.
authorJason Merrill <jason@redhat.com>
Sun, 12 Jan 2020 20:09:22 +0000 (15:09 -0500)
committerJason Merrill <jason@redhat.com>
Mon, 13 Jan 2020 17:50:12 +0000 (12:50 -0500)
My earlier patch to reintroduce short_shift failed to adjust for the C++
front end use of const_op1 rather than op1.

* typeck.c (cp_build_binary_op): Use folded op1 for short_shift.

gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/g++.dg/expr/rshift1.C [new file with mode: 0644]

index 1dad238028acb84af68feff2019259d738cbc473..9b226b84e73a82761b16b7cb16225eba9a845f78 100644 (file)
@@ -1,3 +1,8 @@
+2020-01-13  Jason Merrill  <jason@redhat.com>
+
+       PR c++/93238 - short right-shift with enum.
+       * typeck.c (cp_build_binary_op): Use folded op1 for short_shift.
+
 2020-01-10  Jason Merrill  <jason@redhat.com>
 
        * typeck.c (cp_build_binary_op): Restore short_shift code.
index 7b653cebca0a5da760e732491b73371d43a4c80a..8955442349f903632fc06f7f586a1f05c97b0cff 100644 (file)
@@ -4848,7 +4848,7 @@ cp_build_binary_op (const op_location_t &location,
                }
              else
                {
-                 if (!integer_zerop (op1))
+                 if (!integer_zerop (const_op1))
                    short_shift = 1;
 
                  if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
@@ -5599,6 +5599,7 @@ cp_build_binary_op (const op_location_t &location,
        {
          int unsigned_arg;
          tree arg0 = get_narrower (op0, &unsigned_arg);
+         tree const_op1 = cp_fold_rvalue (op1);
 
          final_type = result_type;
 
@@ -5606,10 +5607,11 @@ cp_build_binary_op (const op_location_t &location,
            unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
 
          if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
-             && tree_int_cst_sgn (op1) > 0
+             && tree_int_cst_sgn (const_op1) > 0
              /* We can shorten only if the shift count is less than the
                 number of bits in the smaller type size.  */
-             && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
+             && compare_tree_int (const_op1,
+                                  TYPE_PRECISION (TREE_TYPE (arg0))) < 0
              /* We cannot drop an unsigned shift after sign-extension.  */
              && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
            {
diff --git a/gcc/testsuite/g++.dg/expr/rshift1.C b/gcc/testsuite/g++.dg/expr/rshift1.C
new file mode 100644 (file)
index 0000000..2f81c02
--- /dev/null
@@ -0,0 +1,8 @@
+// PR c++/93238
+
+short s;
+enum { zero };
+int fn(int i)
+{
+  return s >> zero;
+}