public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] tree-optimization/104942: Retain sizetype conversions till the end
@ 2022-03-16  8:26 Siddhesh Poyarekar
  2022-03-16  8:33 ` Jakub Jelinek
  0 siblings, 1 reply; 2+ messages in thread
From: Siddhesh Poyarekar @ 2022-03-16  8:26 UTC (permalink / raw)
  To: gcc-patches; +Cc: jakub

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:

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

gcc/testsuite/ChangeLog:

	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>
---

Testing:

- i686 build and check
- x86_64 bootstrap build and check
- --with-build-config=bootstrap-ubsan

 .../gcc.dg/builtin-dynamic-object-size-0.c    | 22 +++++++++++++++++++
 gcc/tree-object-size.cc                       |  5 +----
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c
index dd8dc99a580..2fca0a9c5b4 100644
--- a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c
+++ b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c
@@ -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)
diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc
index 8be0df6ba40..9728f79da75 100644
--- a/gcc/tree-object-size.cc
+++ b/gcc/tree-object-size.cc
@@ -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);
 }
 
 
-- 
2.35.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] tree-optimization/104942: Retain sizetype conversions till the end
  2022-03-16  8:26 [PATCH] tree-optimization/104942: Retain sizetype conversions till the end Siddhesh Poyarekar
@ 2022-03-16  8:33 ` Jakub Jelinek
  0 siblings, 0 replies; 2+ messages in thread
From: Jakub Jelinek @ 2022-03-16  8:33 UTC (permalink / raw)
  To: Siddhesh Poyarekar; +Cc: gcc-patches

On Wed, Mar 16, 2022 at 01:56:50PM +0530, Siddhesh Poyarekar wrote:
> 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:
> 
> 	tree-optimization/104942
> 	* tree-object-size.cc (alloc_object_size): Remove STRIP_NOPS.
> 
> gcc/testsuite/ChangeLog:
> 
> 	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>

Ok, thanks.

	Jakub


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-03-16  8:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-16  8:26 [PATCH] tree-optimization/104942: Retain sizetype conversions till the end Siddhesh Poyarekar
2022-03-16  8:33 ` Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).