public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Teach vrp that THIS pointer and references are non-zero
@ 2015-04-13 10:12 Jan Hubicka
  2015-04-13 10:22 ` Jakub Jelinek
  2015-04-13 11:48 ` Richard Biener
  0 siblings, 2 replies; 6+ messages in thread
From: Jan Hubicka @ 2015-04-13 10:12 UTC (permalink / raw)
  To: gcc-patches, rguenther, jason

Hi,
with multiple inheritance compiling the testcase bellow, the first call always
leads to call with offseted pointer, while the other call results in if
conditoinal testing if parameter is non-NULL.

This patch teach VRP that THIS pointers and references are non-zero.  I hope
this is true also for fortran and ada, but I think both languages do not really
have NULL.

I think VRP could basically assume all reference pointers to be non-zero, but
I am not sure how to do that with GIMPLE useless conversion rules.

Bootstrapped/regtested x86_64-linux, OK?

Honza

	* g++.dg/tree-ssa/nonzero-3.C: New testcase.
	* tree-vrp.c (nonnull_arg_p): THIS pointers and references are non-zero.
	(gimple_stmt_nonzero_warnv_p): Reference return values are non-zero.
Index: testsuite/g++.dg/tree-ssa/nonzero-3.C
===================================================================
--- testsuite/g++.dg/tree-ssa/nonzero-3.C	(revision 0)
+++ testsuite/g++.dg/tree-ssa/nonzero-3.C	(working copy)
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-vrp1 -fdelete-null-pointer-checks" } */
+struct A {int a;};
+struct B {int b;};
+struct C:A,B {int c;
+  void bar();};
+
+void foo (struct B *);
+void C::bar ()
+{
+  struct C *d = this;
+  foo(this);
+  foo(d);
+}
+void bar (struct C &c)
+{
+  struct C *d = &c;
+  foo(&c);
+  foo(d);
+}
+/* { dg-final { scan-tree-dump-not "if \\(" "vrp1"} } */
+/* { dg-final { cleanup-tree-dump "vrp1" } } */
Index: tree-vrp.c
===================================================================
--- tree-vrp.c	(revision 222016)
+++ tree-vrp.c	(working copy)
@@ -393,6 +393,17 @@ nonnull_arg_p (const_tree arg)
   if (arg == cfun->static_chain_decl)
     return true;
 
+  /* THIS argument of method is always non-NULL.  */
+  if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
+      && arg == DECL_ARGUMENTS (current_function_decl)
+      && flag_delete_null_pointer_checks)
+    return true;
+
+  /* Values passed by reference are always non-NULL.  */
+  if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
+      && flag_delete_null_pointer_checks)
+    return true;
+
   fntype = TREE_TYPE (current_function_decl);
   for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
     {
@@ -1216,6 +1227,10 @@ gimple_stmt_nonzero_warnv_p (gimple stmt
 	    && DECL_IS_OPERATOR_NEW (fndecl)
 	    && !TREE_NOTHROW (fndecl))
 	  return true;
+	/* Referneces are alwyas non-NULL.  */
+	if (flag_delete_null_pointer_checks
+	    && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
+	  return true;
 	if (flag_delete_null_pointer_checks && 
 	    lookup_attribute ("returns_nonnull",
 			      TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))

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

* Re: Teach vrp that THIS pointer and references are non-zero
  2015-04-13 10:12 Teach vrp that THIS pointer and references are non-zero Jan Hubicka
@ 2015-04-13 10:22 ` Jakub Jelinek
  2015-04-13 11:48 ` Richard Biener
  1 sibling, 0 replies; 6+ messages in thread
From: Jakub Jelinek @ 2015-04-13 10:22 UTC (permalink / raw)
  To: Jan Hubicka; +Cc: gcc-patches, rguenther, jason

On Mon, Apr 13, 2015 at 12:12:49PM +0200, Jan Hubicka wrote:
> @@ -1216,6 +1227,10 @@ gimple_stmt_nonzero_warnv_p (gimple stmt
>  	    && DECL_IS_OPERATOR_NEW (fndecl)
>  	    && !TREE_NOTHROW (fndecl))
>  	  return true;
> +	/* Referneces are alwyas non-NULL.  */

Two typos.

I'll defer the patch review to richi/jason; as far as ubsan is concerned,
the flag_delete_null_pointer_checks guards should hopefully make sure we are
ok.

	Jakub

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

* Re: Teach vrp that THIS pointer and references are non-zero
  2015-04-13 10:12 Teach vrp that THIS pointer and references are non-zero Jan Hubicka
  2015-04-13 10:22 ` Jakub Jelinek
@ 2015-04-13 11:48 ` Richard Biener
  2015-04-13 12:17   ` Jan Hubicka
  1 sibling, 1 reply; 6+ messages in thread
From: Richard Biener @ 2015-04-13 11:48 UTC (permalink / raw)
  To: Jan Hubicka; +Cc: gcc-patches, jason

On Mon, 13 Apr 2015, Jan Hubicka wrote:

> Hi,
> with multiple inheritance compiling the testcase bellow, the first call always
> leads to call with offseted pointer, while the other call results in if
> conditoinal testing if parameter is non-NULL.
> 
> This patch teach VRP that THIS pointers and references are non-zero.  I hope
> this is true also for fortran and ada, but I think both languages do not really
> have NULL.
> 
> I think VRP could basically assume all reference pointers to be non-zero, but
> I am not sure how to do that with GIMPLE useless conversion rules.
> 
> Bootstrapped/regtested x86_64-linux, OK?

Hmm - doesn't ESRA remove unused this eventually so this falls apart?

Thanks,
Richard.

> Honza
> 
> 	* g++.dg/tree-ssa/nonzero-3.C: New testcase.
> 	* tree-vrp.c (nonnull_arg_p): THIS pointers and references are non-zero.
> 	(gimple_stmt_nonzero_warnv_p): Reference return values are non-zero.
> Index: testsuite/g++.dg/tree-ssa/nonzero-3.C
> ===================================================================
> --- testsuite/g++.dg/tree-ssa/nonzero-3.C	(revision 0)
> +++ testsuite/g++.dg/tree-ssa/nonzero-3.C	(working copy)
> @@ -0,0 +1,22 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-vrp1 -fdelete-null-pointer-checks" } */
> +struct A {int a;};
> +struct B {int b;};
> +struct C:A,B {int c;
> +  void bar();};
> +
> +void foo (struct B *);
> +void C::bar ()
> +{
> +  struct C *d = this;
> +  foo(this);
> +  foo(d);
> +}
> +void bar (struct C &c)
> +{
> +  struct C *d = &c;
> +  foo(&c);
> +  foo(d);
> +}
> +/* { dg-final { scan-tree-dump-not "if \\(" "vrp1"} } */
> +/* { dg-final { cleanup-tree-dump "vrp1" } } */
> Index: tree-vrp.c
> ===================================================================
> --- tree-vrp.c	(revision 222016)
> +++ tree-vrp.c	(working copy)
> @@ -393,6 +393,17 @@ nonnull_arg_p (const_tree arg)
>    if (arg == cfun->static_chain_decl)
>      return true;
>  
> +  /* THIS argument of method is always non-NULL.  */
> +  if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
> +      && arg == DECL_ARGUMENTS (current_function_decl)
> +      && flag_delete_null_pointer_checks)
> +    return true;
> +
> +  /* Values passed by reference are always non-NULL.  */
> +  if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
> +      && flag_delete_null_pointer_checks)
> +    return true;
> +
>    fntype = TREE_TYPE (current_function_decl);
>    for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
>      {
> @@ -1216,6 +1227,10 @@ gimple_stmt_nonzero_warnv_p (gimple stmt
>  	    && DECL_IS_OPERATOR_NEW (fndecl)
>  	    && !TREE_NOTHROW (fndecl))
>  	  return true;
> +	/* Referneces are alwyas non-NULL.  */
> +	if (flag_delete_null_pointer_checks
> +	    && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
> +	  return true;
>  	if (flag_delete_null_pointer_checks && 
>  	    lookup_attribute ("returns_nonnull",
>  			      TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
> 
> 

-- 
Richard Biener <rguenther@suse.de>
SUSE LINUX GmbH, GF: Felix Imendoerffer, Jane Smithard, Jennifer Guild,
Dilip Upmanyu, Graham Norton HRB 21284 (AG Nuernberg)

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

* Re: Teach vrp that THIS pointer and references are non-zero
  2015-04-13 11:48 ` Richard Biener
@ 2015-04-13 12:17   ` Jan Hubicka
  2015-04-13 12:18     ` Richard Biener
  0 siblings, 1 reply; 6+ messages in thread
From: Jan Hubicka @ 2015-04-13 12:17 UTC (permalink / raw)
  To: Richard Biener; +Cc: Jan Hubicka, gcc-patches, jason

> On Mon, 13 Apr 2015, Jan Hubicka wrote:
> 
> > Hi,
> > with multiple inheritance compiling the testcase bellow, the first call always
> > leads to call with offseted pointer, while the other call results in if
> > conditoinal testing if parameter is non-NULL.
> > 
> > This patch teach VRP that THIS pointers and references are non-zero.  I hope
> > this is true also for fortran and ada, but I think both languages do not really
> > have NULL.
> > 
> > I think VRP could basically assume all reference pointers to be non-zero, but
> > I am not sure how to do that with GIMPLE useless conversion rules.
> > 
> > Bootstrapped/regtested x86_64-linux, OK?
> 
> Hmm - doesn't ESRA remove unused this eventually so this falls apart?

It changes METHOD_TYPE to FUNCTION_TYPE then.

Honza

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

* Re: Teach vrp that THIS pointer and references are non-zero
  2015-04-13 12:17   ` Jan Hubicka
@ 2015-04-13 12:18     ` Richard Biener
  2015-04-15 13:07       ` Jason Merrill
  0 siblings, 1 reply; 6+ messages in thread
From: Richard Biener @ 2015-04-13 12:18 UTC (permalink / raw)
  To: Jan Hubicka; +Cc: gcc-patches, jason

On Mon, 13 Apr 2015, Jan Hubicka wrote:

> > On Mon, 13 Apr 2015, Jan Hubicka wrote:
> > 
> > > Hi,
> > > with multiple inheritance compiling the testcase bellow, the first call always
> > > leads to call with offseted pointer, while the other call results in if
> > > conditoinal testing if parameter is non-NULL.
> > > 
> > > This patch teach VRP that THIS pointers and references are non-zero.  I hope
> > > this is true also for fortran and ada, but I think both languages do not really
> > > have NULL.
> > > 
> > > I think VRP could basically assume all reference pointers to be non-zero, but
> > > I am not sure how to do that with GIMPLE useless conversion rules.
> > > 
> > > Bootstrapped/regtested x86_64-linux, OK?
> > 
> > Hmm - doesn't ESRA remove unused this eventually so this falls apart?
> 
> It changes METHOD_TYPE to FUNCTION_TYPE then.

I see.  Patch is ok then with Jakubs fixes.

Thanks,
Richard.

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

* Re: Teach vrp that THIS pointer and references are non-zero
  2015-04-13 12:18     ` Richard Biener
@ 2015-04-15 13:07       ` Jason Merrill
  0 siblings, 0 replies; 6+ messages in thread
From: Jason Merrill @ 2015-04-15 13:07 UTC (permalink / raw)
  To: Richard Biener, Jan Hubicka; +Cc: gcc-patches

Yep, looks good.

Jason

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

end of thread, other threads:[~2015-04-15 13:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-13 10:12 Teach vrp that THIS pointer and references are non-zero Jan Hubicka
2015-04-13 10:22 ` Jakub Jelinek
2015-04-13 11:48 ` Richard Biener
2015-04-13 12:17   ` Jan Hubicka
2015-04-13 12:18     ` Richard Biener
2015-04-15 13:07       ` Jason Merrill

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