From b59c0337795fd8101d47a5fbe1389872d5e7addc Mon Sep 17 00:00:00 2001
From: Iain Sandoe <iain@sandoe.co.uk>
Date: Mon, 7 Feb 2022 15:36:35 +0000
Subject: [PATCH] LRA, rs6000, Darwin: Amend lo_sum use for forced constants
 [PR104117].

Two issues resulted in this PR, which manifests when we force a constant into
memory in LRA (in PIC code on Darwin).  The presence of such forced constants
is quite dependent on other RTL optimisations, and it is easy for the issue to
become latent for a specific case.

First, in the Darwin-specific rs6000 backend code, we were not being careful
enough in rejecting invalid symbolic addresses.  Specifically, when generating
PIC code, we require a SYMBOL_REF to be wrapped in an UNSPEC_MACHOPIC_OFFSET.

Second, LRA was attempting to load a register using an invalid lo_sum address.

Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
Co-authored-by: Vladimir Makarov <vmakarov@redhat.com>

	PR target/104117

gcc/ChangeLog:

	* config/rs6000/rs6000.cc (darwin_rs6000_legitimate_lo_sum_const_p):
	Check for UNSPEC_MACHOPIC_OFFSET wrappers on symbolic addresses when
	emitting PIC code.
	(legitimate_lo_sum_address_p): Likewise.
	* lra-constraints.cc (process_address_1): Do not attempt to emit a reg
	load from an invalid lo_sum address.

(cherry picked from commit 4c3792d448964f7bd99e7eac2c29c9eb7c2bfb84)
Signed-off-by: Kirill A. Korinsky <kirill@korins.ky>
---
 gcc/lra-constraints.c | 17 ++---------------
 1 file changed, 2 insertions(+), 15 deletions(-)

diff --git gcc/lra-constraints.c gcc/lra-constraints.c
index 30d088afbca..a19ebcbf261 100644
--- gcc/lra-constraints.c
+++ gcc/lra-constraints.c
@@ -3517,21 +3517,8 @@ process_address_1 (int nop, bool check_only_p,
 		  *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
 		  if (!valid_address_p (op, &ad, cn))
 		    {
-		      /* Try to put lo_sum into register.  */
-		      insn = emit_insn (gen_rtx_SET
-					(new_reg,
-					 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
-		      code = recog_memoized (insn);
-		      if (code >= 0)
-			{
-			  *ad.inner = new_reg;
-			  if (!valid_address_p (op, &ad, cn))
-			    {
-			      *ad.inner = addr;
-			      code = -1;
-			    }
-			}
-
+		      *ad.inner = addr; /* Punt.  */
+		      code = -1;
 		    }
 		}
 	      if (code < 0)
-- 
2.42.1

