aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2018-10-12 19:33:25 +0200
committerJakub Jelinek <jakub@gcc.gnu.org>2018-10-12 19:33:25 +0200
commit3176632ecd20af3d433edb1da834b08bcf0e0e61 (patch)
tree18b9a31b4fcd805b9fc0fba87257743e78b6c16b
parent7685fb062cc50bad395f10d9e98084bbd21605f8 (diff)
downloadgcc-3176632ecd20af3d433edb1da834b08bcf0e0e61.zip
gcc-3176632ecd20af3d433edb1da834b08bcf0e0e61.tar.gz
gcc-3176632ecd20af3d433edb1da834b08bcf0e0e61.tar.bz2
backport: re PR middle-end/87248 (Bad code for masked operations involving signed ints)
Backported from mainline 2018-09-12 Jakub Jelinek <jakub@redhat.com> PR middle-end/87248 * fold-const.c (fold_ternary_loc) <case COND_EXPR>: Verify also that BIT_AND_EXPR's second operand is a power of two. Formatting fix. * c-c++-common/torture/pr87248.c: New test. From-SVN: r265121
-rw-r--r--gcc/ChangeLog6
-rw-r--r--gcc/fold-const.c12
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/c-c++-common/torture/pr87248.c36
4 files changed, 56 insertions, 3 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 8a5d1f3..d579428 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,6 +1,12 @@
2018-10-12 Jakub Jelinek <jakub@redhat.com>
Backported from mainline
+ 2018-09-12 Jakub Jelinek <jakub@redhat.com>
+
+ PR middle-end/87248
+ * fold-const.c (fold_ternary_loc) <case COND_EXPR>: Verify also that
+ BIT_AND_EXPR's second operand is a power of two. Formatting fix.
+
2018-08-27 Jakub Jelinek <jakub@redhat.com>
PR rtl-optimization/87065
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index 6cc04ac..13d27d7 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -11866,10 +11866,16 @@ fold_ternary_loc (location_t loc, enum tree_code code, tree type,
&& integer_pow2p (arg1)
&& TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
&& operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
- arg1, OEP_ONLY_CONST))
+ arg1, OEP_ONLY_CONST)
+ /* operand_equal_p compares just value, not precision, so e.g.
+ arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
+ second operand 32-bit -128, which is not a power of two (or vice
+ versa. */
+ && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
return pedantic_non_lvalue_loc (loc,
- fold_convert_loc (loc, type,
- TREE_OPERAND (arg0, 0)));
+ fold_convert_loc (loc, type,
+ TREE_OPERAND (arg0,
+ 0)));
/* Disable the transformations below for vectors, since
fold_binary_op_with_conditional_arg may undo them immediately,
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 41f3b0b..af7055f 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,6 +1,11 @@
2018-10-12 Jakub Jelinek <jakub@redhat.com>
Backported from mainline
+ 2018-09-12 Jakub Jelinek <jakub@redhat.com>
+
+ PR middle-end/87248
+ * c-c++-common/torture/pr87248.c: New test.
+
2018-08-27 Jakub Jelinek <jakub@redhat.com>
PR rtl-optimization/87065
diff --git a/gcc/testsuite/c-c++-common/torture/pr87248.c b/gcc/testsuite/c-c++-common/torture/pr87248.c
new file mode 100644
index 0000000..6f89bc9
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/torture/pr87248.c
@@ -0,0 +1,36 @@
+/* PR middle-end/87248 */
+/* { dg-do run } */
+
+void
+foo (signed char *p, int q)
+{
+ *p = q & (-__SCHAR_MAX__ - 1) ? (-__SCHAR_MAX__ - 1) : 0;
+}
+
+int
+bar (long long x)
+{
+ return x & (-__INT_MAX__ - 1) ? (-__INT_MAX__ - 1) : 0;
+}
+
+int
+main ()
+{
+#if __INT_MAX__ > 4 * __SCHAR_MAX__
+ signed char a[4];
+ foo (a, __SCHAR_MAX__ + 1U);
+ foo (a + 1, 2 * (__SCHAR_MAX__ + 1U));
+ foo (a + 2, -__INT_MAX__ - 1);
+ foo (a + 3, (__SCHAR_MAX__ + 1U) / 2);
+ if (a[0] != (-__SCHAR_MAX__ - 1) || a[1] != a[0] || a[2] != a[0] || a[3] != 0)
+ __builtin_abort ();
+#endif
+#if __LONG_LONG_MAX__ > 4 * __INT_MAX__
+ if (bar (__INT_MAX__ + 1LL) != (-__INT_MAX__ - 1)
+ || bar (2 * (__INT_MAX__ + 1LL)) != (-__INT_MAX__ - 1)
+ || bar (-__LONG_LONG_MAX__ - 1) != (-__INT_MAX__ - 1)
+ || bar ((__INT_MAX__ + 1LL) / 2) != 0)
+ __builtin_abort ();
+#endif
+ return 0;
+}