tree-optimization/104942: Retain sizetype conversions till the end
authorSiddhesh Poyarekar <siddhesh@gotplt.org>
Wed, 16 Mar 2022 10:40:51 +0000 (16:10 +0530)
committerSiddhesh Poyarekar <siddhesh@gotplt.org>
Wed, 16 Mar 2022 10:40:51 +0000 (16:10 +0530)
Retain the sizetype alloc_object_size to guarantee the assertion in
size_for_offset and to avoid adding a conversion there.  nop conversions
are eliminated at the end anyway in dynamic object size computation.

gcc/ChangeLog:

PR tree-optimization/104942
* tree-object-size.cc (alloc_object_size): Remove STRIP_NOPS.

gcc/testsuite/ChangeLog:

PR tree-optimization/104942
* gcc.dg/builtin-dynamic-object-size-0.c (alloc_func_long,
test_builtin_malloc_long): New functions.
(main): Use it.

Signed-off-by: Siddhesh Poyarekar <siddhesh@gotplt.org>
gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c
gcc/tree-object-size.cc

index dd8dc99a5807c54accb745350ba6ccd21239f24f..2fca0a9c5b418b45891d53d24d16f89b48862a9c 100644 (file)
@@ -4,6 +4,15 @@
 typedef __SIZE_TYPE__ size_t;
 #define abort __builtin_abort
 
+void *
+__attribute__ ((alloc_size (1)))
+__attribute__ ((__nothrow__ , __leaf__))
+__attribute__ ((noinline))
+alloc_func_long (long sz)
+{
+  return __builtin_malloc (sz);
+}
+
 void *
 __attribute__ ((alloc_size (1)))
 __attribute__ ((__nothrow__ , __leaf__))
@@ -145,6 +154,16 @@ test_builtin_malloc_condphi5 (size_t sz, int cond, char *c)
   return ret;
 }
 
+long
+__attribute__ ((noinline))
+test_builtin_malloc_long (long sz, long off)
+{
+  char *a = alloc_func_long (sz);
+  char *dest = a + off;
+  long ret = __builtin_dynamic_object_size (dest, 0);
+  return ret;
+}
+
 /* Calloc-like allocator.  */
 
 size_t
@@ -419,6 +438,9 @@ main (int argc, char **argv)
     FAIL ();
   if (test_builtin_malloc_condphi5 (128, 0, argv[0]) != -1)
     FAIL ();
+  long x = 42;
+  if (test_builtin_malloc_long (x, 0) != x)
+    FAIL ();
   if (test_calloc (2048, 4) != 2048 * 4)
     FAIL ();
   if (test_builtin_calloc (2048, 8) != 2048 * 8)
index 8be0df6ba40f47614b634462b87e9b67578e01b7..9728f79da75345fc0d83192d18f8143b2940538f 100644 (file)
@@ -784,10 +784,7 @@ alloc_object_size (const gcall *call, int object_size_type)
   else if (arg1 >= 0)
     bytes = fold_convert (sizetype, gimple_call_arg (call, arg1));
 
-  if (bytes)
-    return STRIP_NOPS (bytes);
-
-  return size_unknown (object_size_type);
+  return bytes ? bytes : size_unknown (object_size_type);
 }