tree-optimization/110434 - avoid <retval> ={v} {CLOBBER} from NRV

Message ID 20230628102228.5F11E3857C51@sourceware.org
State Accepted
Headers
Series tree-optimization/110434 - avoid <retval> ={v} {CLOBBER} from NRV |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Richard Biener June 28, 2023, 10:21 a.m. UTC
  When NRV replaces a local variable with <retval> it also replaces
occurences in clobbers.  This leads to <retval> being clobbered
before the return of it which is strictly invalid but harmless in
practice since there's no pass after NRV which would remove
earlier stores.

The following fixes this nevertheless.

Bootstrapped and tested on x86_64-unknown-linux-gnu, OK?

Thanks,
Richard.

	PR tree-optimization/110434
	* tree-nrv.cc (pass_nrv::execute): Remove CLOBBERs of
	VAR we replace with <retval>.
---
 gcc/tree-nrv.cc | 8 ++++++++
 1 file changed, 8 insertions(+)
  

Patch

diff --git a/gcc/tree-nrv.cc b/gcc/tree-nrv.cc
index ff47439647c..466b491e4e7 100644
--- a/gcc/tree-nrv.cc
+++ b/gcc/tree-nrv.cc
@@ -256,6 +256,14 @@  pass_nrv::execute (function *fun)
 	      gsi_remove (&gsi, true);
 	      release_defs (stmt);
 	    }
+	  /* If this is a CLOBBER of VAR, remove it.  */
+	  else if (gimple_clobber_p (stmt)
+		   && gimple_assign_lhs (stmt) == found)
+	    {
+	      unlink_stmt_vdef (stmt);
+	      gsi_remove (&gsi, true);
+	      release_defs (stmt);
+	    }
 	  else
 	    {
 	      struct walk_stmt_info wi;