On Wed, 2020-01-29 at 10:00 +0100, Richard Biener wrote:
> 
> P2 please (make _b_o_s const).  No idea why it wasn't that way in the first
> place.  As you say the value-dependence on the result keeps everything
> live and stores or loads are completely irrelevant to the size of objects.
> 
> So OK for P2.
Here's the patch I committed for the historical record.  It just adds
the test and ChangeLog entries relative to the RFA/RFC originally
posted.

Jeff
commit 786083766459723b790405c9ba22f974f84f637e
Author: Jeff Law <l...@redhat.com>
Date:   Wed Jan 29 12:23:53 2020 -0700

    Improve DSE which in turn eliminates the need for jump threading and block
    duplication for the original testcase in pr89689 which in turn eliminates
    the false positive -Warray-bounds warning for the original testcase.
    
            PR tree-optimization/89689
            * builtins.def (BUILT_IN_OBJECT_SIZE): Make it const rather than 
pure.
    
            PR tree-optimization/89689
            * gcc.dg/pr89689.c: New test.

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index e2838c053a3..567dff632f5 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,8 @@
+2020-01-24  Jeff Law  <l...@redhat.com>
+
+       PR tree-optimization/89689
+       * builtins.def (BUILT_IN_OBJECT_SIZE): Make it const rather than pure.
+
 2020-01-29  Richard Sandiford  <richard.sandif...@arm.com>
 
        Revert:
diff --git a/gcc/builtins.def b/gcc/builtins.def
index 5ab842c34c2..fa8b0641ab1 100644
--- a/gcc/builtins.def
+++ b/gcc/builtins.def
@@ -972,7 +972,7 @@ DEF_BUILTIN_STUB (BUILT_IN_STRCMP_EQ, "__builtin_strcmp_eq")
 DEF_BUILTIN_STUB (BUILT_IN_STRNCMP_EQ, "__builtin_strncmp_eq")
 
 /* Object size checking builtins.  */
-DEF_GCC_BUILTIN               (BUILT_IN_OBJECT_SIZE, "object_size", 
BT_FN_SIZE_CONST_PTR_INT, ATTR_PURE_NOTHROW_LEAF_LIST)
+DEF_GCC_BUILTIN               (BUILT_IN_OBJECT_SIZE, "object_size", 
BT_FN_SIZE_CONST_PTR_INT, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_MEMCPY_CHK, "__memcpy_chk", 
BT_FN_PTR_PTR_CONST_PTR_SIZE_SIZE, ATTR_RET1_NOTHROW_NONNULL_LEAF)
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_MEMMOVE_CHK, "__memmove_chk", 
BT_FN_PTR_PTR_CONST_PTR_SIZE_SIZE, ATTR_RET1_NOTHROW_NONNULL_LEAF)
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_MEMPCPY_CHK, "__mempcpy_chk", 
BT_FN_PTR_PTR_CONST_PTR_SIZE_SIZE, ATTR_RETNONNULL_NOTHROW_LEAF)
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index e38c4da8d72..b62e7effb59 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2020-01-29  Jeff Law  <l...@redhat.com
+
+       PR tree-optimization/89689
+       * gcc.dg/pr89689.c: New test.
+
 2020-01-29  Marek Polacek  <pola...@redhat.com>
 
        PR c++/91754 - Fix template arguments comparison with class NTTP.
diff --git a/gcc/testsuite/gcc.dg/pr89689.c b/gcc/testsuite/gcc.dg/pr89689.c
new file mode 100644
index 00000000000..ee81274d3c4
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr89689.c
@@ -0,0 +1,43 @@
+/* { dg-do-compile } */
+/* { dg-options "-O2 -Warray-bounds" } */
+
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+
+static inline __attribute__((__artificial__)) void *a(char *c, const char *d, 
long n)
+{
+    return __builtin___memcpy_chk(c, d, n, __builtin_object_size(c, 0));
+}
+typedef struct {
+    char *data;
+    int len;
+} sb_t;
+const char __sb_slop[1];
+static void inline set0(sb_t *c)
+{
+    if (c->data != __sb_slop)
+        c->data[0] = 0;
+    else
+        assert (c->data[0] == 0);
+}
+char buf[5];
+sb_t l = {
+    .data = buf,
+    .len = 0
+};
+void o()
+{
+    char *data = "abcd";
+    sb_t h = l;
+    set0(&h);
+    a(h.data, data, strlen(data));
+    printf("%s\n", h.data);
+    printf("%d\n", h.data == __sb_slop);
+    printf("%d\n", h.data == buf);
+    set0(&h);
+}
+int main(void) {
+    o();
+    return 0;
+}

Reply via email to