]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
backport: [multiple changes]
authorRichard Biener <rguenther@suse.de>
Mon, 18 Sep 2017 11:07:50 +0000 (11:07 +0000)
committerRichard Biener <rguenth@gcc.gnu.org>
Mon, 18 Sep 2017 11:07:50 +0000 (11:07 +0000)
2017-09-18  Richard Biener  <rguenther@suse.de>

Backport from mainline
2017-08-28  Richard Biener  <rguenther@suse.de>

PR tree-optimization/81977
* tree-ssa-sccvn.c (vn_reference_lookup_3): Fix look through
memcpy.

* g++.dg/torture/pr81977.C: New testcase.

2017-09-04  Richard Biener  <rguenther@suse.de>

PR tree-optimization/82084
* fold-const.h (can_native_encode_string_p): Declare.
* fold-const.c (can_native_encode_string_p): Factor out from ...
(native_encode_string): ... here.
* tree-vect-stmts.c (vectorizable_store): Call it to avoid
vectorizing stores from constants we later cannot handle.

* g++.dg/torture/pr82084.C: New testcase.

2017-07-25  Richard Biener  <rguenther@suse.de>

PR middle-end/81505
* fold-const.c (fold_negate_const): TREE_OVERFLOW should be
sticky.

* gcc.dg/ubsan/pr81505.c: New testcase.

From-SVN: r252919

gcc/ChangeLog
gcc/fold-const.c
gcc/fold-const.h
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/torture/pr81977.C [new file with mode: 0644]
gcc/testsuite/g++.dg/torture/pr82084.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/ubsan/pr81505.c [new file with mode: 0644]
gcc/tree-ssa-sccvn.c
gcc/tree-vect-stmts.c

index e765a1f45818a7a55cb7aba239238c61ae8831c7..bdb3c5190b6a283a8baa6c54e9a761f687549577 100644 (file)
@@ -1,3 +1,27 @@
+2017-09-18  Richard Biener  <rguenther@suse.de>
+       Backport from mainline
+       2017-08-28  Richard Biener  <rguenther@suse.de>
+
+       PR tree-optimization/81977
+       * tree-ssa-sccvn.c (vn_reference_lookup_3): Fix look through
+       memcpy.
+
+       2017-09-04  Richard Biener  <rguenther@suse.de>
+
+       PR tree-optimization/82084
+       * fold-const.h (can_native_encode_string_p): Declare.
+       * fold-const.c (can_native_encode_string_p): Factor out from ...
+       (native_encode_string): ... here.
+       * tree-vect-stmts.c (vectorizable_store): Call it to avoid
+       vectorizing stores from constants we later cannot handle.
+
+       2017-07-25  Richard Biener  <rguenther@suse.de>
+
+       PR middle-end/81505
+       * fold-const.c (fold_negate_const): TREE_OVERFLOW should be
+       sticky.
+
 2017-09-15  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
index 9f85e0a6c17f020b88247708ab2ee3753e167a7d..9ba92b012144f1195c3ef9eaf0259a121abfce5a 100644 (file)
@@ -7356,15 +7356,10 @@ native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
 static int
 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
 {
-  tree type = TREE_TYPE (expr);
-  HOST_WIDE_INT total_bytes;
-
-  if (TREE_CODE (type) != ARRAY_TYPE
-      || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
-      || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
-      || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
+  if (! can_native_encode_string_p (expr))
     return 0;
-  total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
+
+  HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
   if ((off == -1 && total_bytes > len)
       || off >= total_bytes)
     return 0;
@@ -7638,6 +7633,22 @@ can_native_interpret_type_p (tree type)
     }
 }
 
+/* Return true iff a STRING_CST S is accepted by
+   native_encode_expr.  */
+
+bool
+can_native_encode_string_p (const_tree expr)
+{
+  tree type = TREE_TYPE (expr);
+
+  if (TREE_CODE (type) != ARRAY_TYPE
+      || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
+      || (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT)
+      || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
+    return false;
+  return true;
+}
+
 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
    TYPE at compile-time.  If we're unable to perform the conversion
    return NULL_TREE.  */
@@ -13980,8 +13991,8 @@ fold_negate_const (tree arg0, tree type)
        bool overflow;
        wide_int val = wi::neg (arg0, &overflow);
        t = force_fit_type (type, val, 1,
-                           (overflow | TREE_OVERFLOW (arg0))
-                           && !TYPE_UNSIGNED (type));
+                           (overflow && ! TYPE_UNSIGNED (type))
+                           || TREE_OVERFLOW (arg0));
        break;
       }
 
index 02f42709c4e0eec08f06311a7f6ebd023775af53..6c2c070d5ffe09754452ac0387b956f0236f4eb7 100644 (file)
@@ -27,6 +27,7 @@ extern int folding_initializer;
 /* Convert between trees and native memory representation.  */
 extern int native_encode_expr (const_tree, unsigned char *, int, int off = -1);
 extern tree native_interpret_expr (tree, const unsigned char *, int);
+extern bool can_native_encode_string_p (const_tree);
 
 /* Fold constants as much as possible in an expression.
    Returns the simplified expression.
index 9d20ad65b2660bc8cb48bac749a1c1fd0ed9f84f..910888ed964c9e5b6387329cd8f9a787a82e6417 100644 (file)
@@ -1,3 +1,21 @@
+2017-09-18  Richard Biener  <rguenther@suse.de>
+       Backport from mainline
+       2017-08-28  Richard Biener  <rguenther@suse.de>
+
+       PR tree-optimization/81977
+       * g++.dg/torture/pr81977.C: New testcase.
+
+       2017-09-04  Richard Biener  <rguenther@suse.de>
+
+       PR tree-optimization/82084
+       * g++.dg/torture/pr82084.C: New testcase.
+
+       2017-07-25  Richard Biener  <rguenther@suse.de>
+
+       PR middle-end/81505
+       * gcc.dg/ubsan/pr81505.c: New testcase.
+
 2017-09-18  Martin Liska  <mliska@suse.cz>
 
        * gcc.dg/asan/pr81224.c: Remove.
diff --git a/gcc/testsuite/g++.dg/torture/pr81977.C b/gcc/testsuite/g++.dg/torture/pr81977.C
new file mode 100644 (file)
index 0000000..a8c8ba0
--- /dev/null
@@ -0,0 +1,55 @@
+/* { dg-do run } */
+/* { dg-require-effective-target int32plus } */
+
+#include <cstdint>
+
+typedef struct
+{
+  uint16_t  x ;
+  uint16_t  y ;
+  uint64_t  z ;
+} __attribute__((packed, aligned(1))) TestMsgType;
+
+struct Payload
+{
+  uint16_t header_info[2];
+  TestMsgType _pref;
+  void Pack(uint8_t *buffer)
+    {
+      __builtin_memcpy(buffer, &_pref, sizeof(_pref));
+    }
+  void UnPack(uint8_t *buffer)
+    {
+      __builtin_memcpy(&_pref, buffer, sizeof(_pref));
+    }
+};
+
+
+struct Msg
+{
+  Payload _payload;
+  void Pack(uint8_t *buffer)
+    {
+      _payload.Pack(buffer);
+    }
+
+  void UnPack(uint8_t *buffer)
+    {
+      _payload.UnPack(buffer);
+    }
+};
+
+int main()
+{
+  uint8_t * buffer = new uint8_t [30];
+  Msg msg;
+  Msg msg1;
+  msg._payload._pref.x             = 0xabcd;
+  msg._payload._pref.y             = 0xa;
+  msg._payload._pref.z             = 0x0001020304051617;
+  msg.Pack(&buffer[0]);
+  msg1.UnPack(&buffer[0]);
+  if (msg1._payload._pref.x != 0xabcd)
+    __builtin_abort ();
+  delete [] buffer;
+}
diff --git a/gcc/testsuite/g++.dg/torture/pr82084.C b/gcc/testsuite/g++.dg/torture/pr82084.C
new file mode 100644 (file)
index 0000000..416684d
--- /dev/null
@@ -0,0 +1,9 @@
+// { dg-do compile }
+
+#include <string>
+int main()
+{
+  wchar_t strs[4][2]= {  L"A", L"B", L"C" , L"D"};
+  std::wstring ss(strs[0]);
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/ubsan/pr81505.c b/gcc/testsuite/gcc.dg/ubsan/pr81505.c
new file mode 100644 (file)
index 0000000..1cebef5
--- /dev/null
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -fsanitize=signed-integer-overflow" } */
+
+int a, b, c, h;
+
+int i[5][5];
+
+void
+fn1 ()
+{
+  int l = 0;
+
+  for (a = 0; a <= 3; a++)
+    for (b = 1; b >= 0; b -= 1)
+      l |= i[0][b];
+  c = l;
+}
index 76f3626b4f4701e8727a056a61a0893d235fd08a..65077ede43cfe6594898725c4b322f01aec331ec 100644 (file)
@@ -2169,7 +2169,7 @@ vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_,
       memset (&op, 0, sizeof (op));
       op.type = vr->type;
       op.opcode = MEM_REF;
-      op.op0 = build_int_cst (ptr_type_node, at - rhs_offset);
+      op.op0 = build_int_cst (ptr_type_node, at - lhs_offset + rhs_offset);
       op.off = at - lhs_offset + rhs_offset;
       vr->operands[0] = op;
       op.type = TREE_TYPE (rhs);
index 81fc46b7b26588f53b7644b8b52872db95ca2ca0..9a2402299f434071dce2e40081044ee02af66d9a 100644 (file)
@@ -5317,6 +5317,12 @@ vectorizable_store (gimple *stmt, gimple_stmt_iterator *gsi, gimple **vec_stmt,
 
   op = gimple_assign_rhs1 (stmt);
 
+  /* In the case this is a store from a STRING_CST make sure
+     native_encode_expr can handle it.  */
+  if (TREE_CODE (op) == STRING_CST
+      && ! can_native_encode_string_p (op))
+    return false;
+
   if (!vect_is_simple_use (op, vinfo, &def_stmt, &dt, &rhs_vectype))
     {
       if (dump_enabled_p ())