public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-5915] Terminate BB analysis on NULL memory access in ipa-pure-const and ipa-modref
@ 2021-12-12 10:38 Jan Hubicka
  0 siblings, 0 replies; only message in thread
From: Jan Hubicka @ 2021-12-12 10:38 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:e93809f62363ba4b233858005aef652fb550e896

commit r12-5915-ge93809f62363ba4b233858005aef652fb550e896
Author: Jan Hubicka <jh@suse.cz>
Date:   Sun Dec 12 11:38:13 2021 +0100

    Terminate BB analysis on NULL memory access in ipa-pure-const and ipa-modref
    
    As discussed in the PR, we miss some optimization becuase
    gimple-ssa-isolate-paths turns NULL memory accesses to volatile and adds
    __builtin_trap after them.  This is seen as a side-effect by IPA analysis
    and additionally the (fully unreachable) builtin_trap is believed to load
    all global memory.
    
    I think we should think of less intrusive gimple representation of this, but
    it is also easy enough to special case that in IPA analysers as done in
    this patch.  This is a win even if we improve the representation since
    gimple-ssa-isolate-paths is run late and this way we improve optimization
    early.
    
    This affects 1623 functions during cc1plus link.
    
    Bootstrapped/regtested x86_64-linux, comitted.
    
    gcc/ChangeLog:
    
    2021-12-12  Jan Hubicka  <hubicka@ucw.cz>
    
            PR ipa/103665
            * ipa-modref.c (modref_access_analysis::analyze): Terminate BB
            analysis on NULL memory access.
            * ipa-pure-const.c (analyze_function): Likewise.

Diff:
---
 gcc/ipa-modref.c     | 13 +++++++++++++
 gcc/ipa-pure-const.c | 22 ++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/gcc/ipa-modref.c b/gcc/ipa-modref.c
index 55fa873e1f0..2c89c63baf6 100644
--- a/gcc/ipa-modref.c
+++ b/gcc/ipa-modref.c
@@ -1750,6 +1750,19 @@ modref_access_analysis::analyze ()
       for (si = gsi_start_nondebug_after_labels_bb (bb);
 	   !gsi_end_p (si); gsi_next_nondebug (&si))
 	{
+	  /* NULL memory accesses terminates BB.  These accesses are known
+	     to trip undefined behaviour.  gimple-ssa-isolate-paths turns them
+	     to volatile accesses and adds builtin_trap call which would
+	     confuse us otherwise.  */
+	  if (infer_nonnull_range_by_dereference (gsi_stmt (si),
+						  null_pointer_node))
+	    {
+	      if (dump_file)
+		fprintf (dump_file, " - NULL memory access; terminating BB\n");
+	      if (flag_non_call_exceptions)
+		set_side_effects ();
+	      break;
+	    }
 	  analyze_stmt (gsi_stmt (si), always_executed);
 
 	  /* Avoid doing useles work.  */
diff --git a/gcc/ipa-pure-const.c b/gcc/ipa-pure-const.c
index fea8b08c4eb..25503f360e6 100644
--- a/gcc/ipa-pure-const.c
+++ b/gcc/ipa-pure-const.c
@@ -1097,6 +1097,28 @@ analyze_function (struct cgraph_node *fn, bool ipa)
 	   !gsi_end_p (gsi);
 	   gsi_next (&gsi))
 	{
+	  /* NULL memory accesses terminates BB.  These accesses are known
+	     to trip undefined behaviour.  gimple-ssa-isolate-paths turns them
+	     to volatile accesses and adds builtin_trap call which would
+	     confuse us otherwise.  */
+	  if (infer_nonnull_range_by_dereference (gsi_stmt (gsi),
+						  null_pointer_node))
+	    {
+	      if (dump_file)
+		fprintf (dump_file, "  NULL memory access; terminating BB%s\n",
+			 flag_non_call_exceptions ? "; looping" : "");
+	      if (flag_non_call_exceptions)
+		{
+		  l->looping = true;
+		  if (stmt_can_throw_external (cfun, gsi_stmt (gsi)))
+		    {
+		      if (dump_file)
+			fprintf (dump_file, "    can throw externally\n");
+		      l->can_throw = true;
+		    }
+		}
+	      break;
+	    }
 	  check_stmt (&gsi, l, ipa);
 	  if (l->pure_const_state == IPA_NEITHER
 	      && l->looping


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-12-12 10:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-12 10:38 [gcc r12-5915] Terminate BB analysis on NULL memory access in ipa-pure-const and ipa-modref Jan Hubicka

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