From patchwork Fri May 24 02:11:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Schmidt X-Patchwork-Id: 1104570 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-501585-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="A2UW1fUP"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4598yW5Lvyz9sCJ for ; Fri, 24 May 2019 12:12:11 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to:cc :from:subject:date:mime-version:message-id:content-type :content-transfer-encoding; q=dns; s=default; b=UApuD9hXwDgp++WW q/BE45xCcXQNUh7PzPVqtq+MrNTY4kHngJmrfs1JM5OkFroY9KDNbttBoR+V9AHd p1bN0WO8A8eZgadceSVRUh+/OAkRvYeIxLq4zrJzZ/8p/d7MAqBqvPX0tfk8lX/1 hqFvSiiakTdvKen0fHtNna71Bxo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to:cc :from:subject:date:mime-version:message-id:content-type :content-transfer-encoding; s=default; bh=3MhxO6ggWQFIh059ugR5PZ 4/6ew=; b=A2UW1fUP0GY6VniuH0ReI1xwcYm4K06FcJDZNIJarggzW0pH1NFDlC y82VoU95v8jHrTZFiKzu2q2L0s51o4ukdGv16vO4iUGSequiE4PoAbIVk0axi3Aw HVgTTKEsP/nbVgf4WMGnLT5eNOhQ/DWWRWdD8jsfKwce16IGqtLU8= Received: (qmail 18580 invoked by alias); 24 May 2019 02:12:00 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 18492 invoked by uid 89); 24 May 2019 02:12:00 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-20.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, HTML_MESSAGE, KAM_STOCKGEN, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.1 spammy=legitimate, Direct, WHICH X-HELO: mx0a-001b2d01.pphosted.com Received: from mx0a-001b2d01.pphosted.com (HELO mx0a-001b2d01.pphosted.com) (148.163.156.1) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 24 May 2019 02:11:54 +0000 Received: from pps.filterd (m0098409.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x4O26lJf124839 for ; Thu, 23 May 2019 22:11:50 -0400 Received: from e14.ny.us.ibm.com (e14.ny.us.ibm.com [129.33.205.204]) by mx0a-001b2d01.pphosted.com with ESMTP id 2sp3k6gfss-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 23 May 2019 22:11:50 -0400 Received: from localhost by e14.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 24 May 2019 03:11:49 +0100 Received: from b01cxnp22034.gho.pok.ibm.com (9.57.198.24) by e14.ny.us.ibm.com (146.89.104.201) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Fri, 24 May 2019 03:11:46 +0100 Received: from b01ledav002.gho.pok.ibm.com (b01ledav002.gho.pok.ibm.com [9.57.199.107]) by b01cxnp22034.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x4O2BjUr32899210 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 24 May 2019 02:11:45 GMT Received: from b01ledav002.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C4B22124052; Fri, 24 May 2019 02:11:45 +0000 (GMT) Received: from b01ledav002.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3A49F124054; Fri, 24 May 2019 02:11:45 +0000 (GMT) Received: from BigMac.local (unknown [9.85.143.85]) by b01ledav002.gho.pok.ibm.com (Postfix) with ESMTP; Fri, 24 May 2019 02:11:45 +0000 (GMT) To: GCC Patches Cc: Segher Boessenkool , Alan Modra From: Bill Schmidt Subject: [PATCH] rs6000: Call flow implementation for PC-relative addressing Date: Thu, 23 May 2019 21:11:44 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 x-cbid: 19052402-0052-0000-0000-000003C64240 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00011152; HX=3.00000242; KW=3.00000007; PH=3.00000004; SC=3.00000286; SDB=6.01207709; UDB=6.00634280; IPR=6.00988690; MB=3.00027025; MTD=3.00000008; XFM=3.00000015; UTC=2019-05-24 02:11:48 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19052402-0053-0000-0000-00006105B6C0 Message-Id: Hi, This patch contains the changes to implement call flow for PC-relative addressing. It's an amalgam of several internal patches that Alan and I worked on, and as a result it's hard to tease apart individual pieces much further. So I apologize that this patch is a little larger than the others. Also, I've CC'd Alan so he can help answer questions about the patch, particularly the PLT bits I'm not very familiar with. Following are descriptions of the individual patches that are combined here. (1) When a function uses PC-relative code generation, all direct calls (other than sibcalls) that the function makes to local or external callees should appear as "bl sym@notoc" and should not be followed by a nop instruction. @notoc indicates that the assembler should annotate the call with R_PPC64_REL24_NOTOC, meaning that the caller does not guarantee that r2 contains a valid TOC pointer. Thus the linker should not try to replace a subsequent "nop" with a TOC restore instruction. I've added a test case for the four cases handled here: local calls with/without a return value, and external cases with/without a return value. (2) If a caller preserves the TOC pointer and the callee does not, or vice versa, then a sibcall will cause an inconsistency. Don't allow that. (3) The linker needs a @notoc directive on sibcall targets when the caller does not provide or preserve a TOC pointer. This patch provides for that. In creating the new sibcall patterns, I did not duplicate the "c" alternatives that allow for bctr or blr sibcalls. I don't think there's a way to generate those currently. The bctr would be legitimate for PC-relative sibcalls if you can prove that the target function is in the same binary, but we don't appear to detect that possibility today. (4) This patch deletes all the extra insns added to handle pcrel calls, instead opting to use existing insns but making their output conditional on rs6000_pcrel_p(cfun). There isn't a need to differentiate between pcrel and non-pcrel calls at the point rtl is created; rs6000_pcrel_p is valid right up to the final pass, as evidenced by use of rs6000_pcrel_p to emit .localentry. There is one case however where we do need new insns: The existing nonlocal indirect call insns mention r2 in their rtl. That isn't correct for pcrel indirect calls, and may cause problems when/if r2 is allocated as any other volatile gpr in pcrel code. The patch also fixes pcrel inline PLT calls (which are used for -fno-plt and -mlongcall) to use a pcrel load from the PLT rather than attempting (and failing) to use TOC-relative loads. This requires some changes in the way relocs are emitted. For prefix insns we can't write .reloc .,R_PPC64_PLT_PCREL34_NOTOC,foo pld 12,0(0),1 since the pld may require a padding nop. Instead it's necessary to put the .reloc after the instruction or use a label on the insn. Like this (which is what the patch does): pld 12,0(0),1 .reloc .-8,R_PPC64_PLT_PCREL34_NOTOC,foo or this: .reloc 0f,R_PPC64_PLT_PCREL34_NOTOC,foo 0: pld 12,0(0),1 Bootstrapped and tested on powerpc64le-unknown-linux-gnu with no regressions. Is this okay for trunk? Thanks! Bill [gcc] 2019-05-23 Bill Schmidt Alan Modra * config/rs6000/rs6000.c (rs6000_call_template_1): Handle pcrel calls here... (rs6000_indirect_call_template_1): ...and here. (rs6000_indirect_sibcall_template): Handle plt_pcrel34. Rework tocsave, plt16_ha, plt16_lo, mtctr indirect calls. (rs6000_decl_ok_for_sibcall): New function. (rs6000_function_ok_for_sibcall): Refactor. (rs6000_longcall_ref): Use UNSPEC_PLT_PCREL when pcrel. (rs6000_call_aix): Don't emit toc restore rtl for indirect calls when pcrel. Reorganize. (rs6000_sibcall_aix): Don't add r2 to function usage when pcrel. * rs6000.md (UNSPEC_PLT_PCREL): New unspec. (*pltseq_plt_pcrel): New insn. (*call_local_aix): Handle @notoc calls. (*call_value_local_aix): Likewise. (*call_nonlocal_aix): Adjust lengths for pcrel calls. (*call_value_nonlocal_aix): Likewise. (*call_indirect_pcrel): New insn. (*call_value_indirect_pcrel): Likewise. [gcc/testsuite] 2019-05-23 Bill Schmidt * gcc.target/powerpc/notoc-direct-1.c: New. * gcc.target/powerpc/pcrel-sibcall-1.c: New. diff --git a/gcc/config/rs6000/rs6000.c b/gcc/config/rs6000/rs6000.c index 3d5cf9e4ece..9229bad6acc 100644 --- a/gcc/config/rs6000/rs6000.c +++ b/gcc/config/rs6000/rs6000.c @@ -21268,7 +21268,9 @@ rs6000_call_template_1 (rtx *operands, unsigned int funop, bool sibcall) ? "+32768" : "")); static char str[32]; /* 2 spare */ - if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2) + if (rs6000_pcrel_p (cfun)) + sprintf (str, "b%s %s@notoc%s", sibcall ? "" : "l", z, arg); + else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2) sprintf (str, "b%s %s%s%s", sibcall ? "" : "l", z, arg, sibcall ? "" : "\n\tnop"); else if (DEFAULT_ABI == ABI_V4) @@ -21333,6 +21335,16 @@ rs6000_indirect_call_template_1 (rtx *operands, unsigned int funop, /* Currently, funop is either 0 or 1. The maximum string is always a !speculate 64-bit __tls_get_addr call. + ABI_ELFv2, pcrel: + . 27 .reloc .,R_PPC64_TLSGD,%2\n\t + . 35 .reloc .,R_PPC64_PLTSEQ_NOTOC,%z1\n\t + . 9 crset 2\n\t + . 27 .reloc .,R_PPC64_TLSGD,%2\n\t + . 36 .reloc .,R_PPC64_PLTCALL_NOTOC,%z1\n\t + . 8 beq%T1l- + .--- + .142 + ABI_AIX: . 9 ld 2,%3\n\t . 27 .reloc .,R_PPC64_TLSGD,%2\n\t @@ -21398,23 +21410,31 @@ rs6000_indirect_call_template_1 (rtx *operands, unsigned int funop, gcc_unreachable (); } + const char *notoc = rs6000_pcrel_p (cfun) ? "_NOTOC" : ""; const char *addend = (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic == 2 ? "+32768" : ""); if (!speculate) { s += sprintf (s, - "%s.reloc .,R_PPC%s_PLTSEQ,%%z%u%s\n\t", - tls, rel64, funop, addend); + "%s.reloc .,R_PPC%s_PLTSEQ%s,%%z%u%s\n\t", + tls, rel64, notoc, funop, addend); s += sprintf (s, "crset 2\n\t"); } s += sprintf (s, - "%s.reloc .,R_PPC%s_PLTCALL,%%z%u%s\n\t", - tls, rel64, funop, addend); + "%s.reloc .,R_PPC%s_PLTCALL%s,%%z%u%s\n\t", + tls, rel64, notoc, funop, addend); } else if (!speculate) s += sprintf (s, "crset 2\n\t"); - if (DEFAULT_ABI == ABI_AIX) + if (rs6000_pcrel_p (cfun)) + { + if (speculate) + sprintf (s, "b%%T%ul", funop); + else + sprintf (s, "beq%%T%ul-", funop); + } + else if (DEFAULT_ABI == ABI_AIX) { if (speculate) sprintf (s, @@ -21468,63 +21488,73 @@ rs6000_indirect_sibcall_template (rtx *operands, unsigned int funop) #if HAVE_AS_PLTSEQ /* Output indirect call insns. - WHICH is 0 for tocsave, 1 for plt16_ha, 2 for plt16_lo, 3 for mtctr. */ + WHICH is 0 for tocsave, 1 for plt16_ha, 2 for plt16_lo, 3 for mtctr, + 4 for plt_pcrel34. */ const char * rs6000_pltseq_template (rtx *operands, int which) { const char *rel64 = TARGET_64BIT ? "64" : ""; - char tls[28]; + char tls[30]; tls[0] = 0; if (TARGET_TLS_MARKERS && GET_CODE (operands[3]) == UNSPEC) { + char off = which == 4 ? '8' : '4'; if (XINT (operands[3], 1) == UNSPEC_TLSGD) - sprintf (tls, ".reloc .,R_PPC%s_TLSGD,%%3\n\t", - rel64); + sprintf (tls, ".reloc .-%c,R_PPC%s_TLSGD,%%3\n\t", + off, rel64); else if (XINT (operands[3], 1) == UNSPEC_TLSLD) - sprintf (tls, ".reloc .,R_PPC%s_TLSLD,%%&\n\t", - rel64); + sprintf (tls, ".reloc .-%c,R_PPC%s_TLSLD,%%&\n\t", + off, rel64); else gcc_unreachable (); } gcc_assert (DEFAULT_ABI == ABI_ELFv2 || DEFAULT_ABI == ABI_V4); - static char str[96]; /* 15 spare */ - const char *off = WORDS_BIG_ENDIAN ? "+2" : ""; + static char str[96]; /* 10 spare */ + char off = WORDS_BIG_ENDIAN ? '2' : '4'; const char *addend = (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic == 2 ? "+32768" : ""); switch (which) { case 0: sprintf (str, - "%s.reloc .,R_PPC%s_PLTSEQ,%%z2\n\t" - "st%s", - tls, rel64, TARGET_64BIT ? "d 2,24(1)" : "w 2,12(1)"); + "st%s\n\t" + "%s.reloc .-4,R_PPC%s_PLTSEQ,%%z2", + TARGET_64BIT ? "d 2,24(1)" : "w 2,12(1)", + tls, rel64); break; case 1: if (DEFAULT_ABI == ABI_V4 && !flag_pic) sprintf (str, - "%s.reloc .%s,R_PPC%s_PLT16_HA,%%z2\n\t" - "lis %%0,0", + "lis %%0,0\n\t" + "%s.reloc .-%c,R_PPC%s_PLT16_HA,%%z2", tls, off, rel64); else sprintf (str, - "%s.reloc .%s,R_PPC%s_PLT16_HA,%%z2%s\n\t" - "addis %%0,%%1,0", + "addis %%0,%%1,0\n\t" + "%s.reloc .-%c,R_PPC%s_PLT16_HA,%%z2%s", tls, off, rel64, addend); break; case 2: sprintf (str, - "%s.reloc .%s,R_PPC%s_PLT16_LO%s,%%z2%s\n\t" - "l%s %%0,0(%%1)", - tls, off, rel64, TARGET_64BIT ? "_DS" : "", addend, - TARGET_64BIT ? "d" : "wz"); + "l%s %%0,0(%%1)\n\t" + "%s.reloc .-%c,R_PPC%s_PLT16_LO%s,%%z2%s", + TARGET_64BIT ? "d" : "wz", + tls, off, rel64, TARGET_64BIT ? "_DS" : "", addend); break; case 3: sprintf (str, - "%s.reloc .,R_PPC%s_PLTSEQ,%%z2%s\n\t" - "mtctr %%1", + "mtctr %%1\n\t" + "%s.reloc .-4,R_PPC%s_PLTSEQ,%%z2%s", tls, rel64, addend); break; + case 4: + sprintf (str, + "pl%s %%0,0(0),1\n\t" + "%s.reloc .-8,R_PPC%s_PLT_PCREL34_NOTOC,%%z2", + TARGET_64BIT ? "d" : "wz", + tls, rel64); + break; default: gcc_unreachable (); } @@ -24703,6 +24733,53 @@ rs6000_return_addr (int count, rtx frame) return get_hard_reg_initial_val (Pmode, LR_REGNO); } +/* Helper function for rs6000_function_ok_for_sibcall. */ + +static bool +rs6000_decl_ok_for_sibcall (tree decl) +{ + /* Sibcalls are always fine for the Darwin ABI. */ + if (DEFAULT_ABI == ABI_DARWIN) + return true; + + if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2) + { + /* Under the AIX or ELFv2 ABIs we can't allow calls to non-local + functions, because the callee may have a different TOC pointer to + the caller and there's no way to ensure we restore the TOC when + we return. */ + if (!decl || DECL_EXTERNAL (decl) || DECL_WEAK (decl) + || !(*targetm.binds_local_p) (decl)) + return false; + + /* Similarly, if the caller preserves the TOC pointer and the callee + doesn't (or vice versa), proper TOC setup or restoration will be + missed. For example, suppose A, B, and C are in the same binary + and A -> B -> C. A and B preserve the TOC pointer but C does not, + and B -> C is eligible as a sibcall. A will call B through its + local entry point, so A will not restore its TOC itself. B calls + C with a sibcall, so it will not restore the TOC. C does not + preserve the TOC, so it may clobber r2 with impunity. Returning + from C will result in a corrupted TOC for A. */ + else if (rs6000_fndecl_pcrel_p (decl) != rs6000_pcrel_p (cfun)) + return false; + + else + return true; + } + + /* With the secure-plt SYSV ABI we can't make non-local calls when + -fpic/PIC because the plt call stubs use r30. */ + if (DEFAULT_ABI == ABI_V4 + && (!TARGET_SECURE_PLT + || !flag_pic + || (decl + && (*targetm.binds_local_p) (decl)))) + return true; + + return false; +} + /* Say whether a function is a candidate for sibcall handling or not. */ static bool @@ -24748,22 +24825,7 @@ rs6000_function_ok_for_sibcall (tree decl, tree exp) return false; } - /* Under the AIX or ELFv2 ABIs we can't allow calls to non-local - functions, because the callee may have a different TOC pointer to - the caller and there's no way to ensure we restore the TOC when - we return. With the secure-plt SYSV ABI we can't make non-local - calls when -fpic/PIC because the plt call stubs use r30. */ - if (DEFAULT_ABI == ABI_DARWIN - || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2) - && decl - && !DECL_EXTERNAL (decl) - && !DECL_WEAK (decl) - && (*targetm.binds_local_p) (decl)) - || (DEFAULT_ABI == ABI_V4 - && (!TARGET_SECURE_PLT - || !flag_pic - || (decl - && (*targetm.binds_local_p) (decl))))) + if (rs6000_decl_ok_for_sibcall (decl)) { tree attr_list = TYPE_ATTRIBUTES (fntype); @@ -32592,12 +32654,18 @@ rs6000_longcall_ref (rtx call_ref, rtx arg) if (TARGET_PLTSEQ) { rtx base = const0_rtx; - int regno; - if (DEFAULT_ABI == ABI_ELFv2) + int regno = 12; + if (rs6000_pcrel_p (cfun)) { - base = gen_rtx_REG (Pmode, TOC_REGISTER); - regno = 12; + rtx reg = gen_rtx_REG (Pmode, regno); + rtx u = gen_rtx_UNSPEC (Pmode, gen_rtvec (3, base, call_ref, arg), + UNSPEC_PLT_PCREL); + emit_insn (gen_rtx_SET (reg, u)); + return reg; } + + if (DEFAULT_ABI == ABI_ELFv2) + base = gen_rtx_REG (Pmode, TOC_REGISTER); else { if (flag_pic) @@ -37706,37 +37774,38 @@ rs6000_call_aix (rtx value, rtx func_desc, rtx tlsarg, rtx cookie) if (!SYMBOL_REF_P (func) || (DEFAULT_ABI == ABI_AIX && !SYMBOL_REF_FUNCTION_P (func))) { - /* Save the TOC into its reserved slot before the call, - and prepare to restore it after the call. */ - rtx stack_toc_offset = GEN_INT (RS6000_TOC_SAVE_SLOT); - rtx stack_toc_unspec = gen_rtx_UNSPEC (Pmode, - gen_rtvec (1, stack_toc_offset), - UNSPEC_TOCSLOT); - toc_restore = gen_rtx_SET (toc_reg, stack_toc_unspec); - - /* Can we optimize saving the TOC in the prologue or - do we need to do it at every call? */ - if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca) - cfun->machine->save_toc_in_prologue = true; - else + if (!rs6000_pcrel_p (cfun)) { - rtx stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM); - rtx stack_toc_mem = gen_frame_mem (Pmode, - gen_rtx_PLUS (Pmode, stack_ptr, - stack_toc_offset)); - MEM_VOLATILE_P (stack_toc_mem) = 1; - if (is_pltseq_longcall) + /* Save the TOC into its reserved slot before the call, + and prepare to restore it after the call. */ + rtx stack_toc_offset = GEN_INT (RS6000_TOC_SAVE_SLOT); + rtx stack_toc_unspec = gen_rtx_UNSPEC (Pmode, + gen_rtvec (1, stack_toc_offset), + UNSPEC_TOCSLOT); + toc_restore = gen_rtx_SET (toc_reg, stack_toc_unspec); + + /* Can we optimize saving the TOC in the prologue or + do we need to do it at every call? */ + if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca) + cfun->machine->save_toc_in_prologue = true; + else { - /* Use USPEC_PLTSEQ here to emit every instruction in an - inline PLT call sequence with a reloc, enabling the - linker to edit the sequence back to a direct call - when that makes sense. */ - rtvec v = gen_rtvec (3, toc_reg, func_desc, tlsarg); - rtx mark_toc_reg = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ); - emit_insn (gen_rtx_SET (stack_toc_mem, mark_toc_reg)); + rtx stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM); + rtx stack_toc_mem = gen_frame_mem (Pmode, + gen_rtx_PLUS (Pmode, stack_ptr, + stack_toc_offset)); + MEM_VOLATILE_P (stack_toc_mem) = 1; + if (HAVE_AS_PLTSEQ + && DEFAULT_ABI == ABI_ELFv2 + && GET_CODE (func_desc) == SYMBOL_REF) + { + rtvec v = gen_rtvec (3, toc_reg, func_desc, tlsarg); + rtx mark_toc_reg = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ); + emit_insn (gen_rtx_SET (stack_toc_mem, mark_toc_reg)); + } + else + emit_move_insn (stack_toc_mem, toc_reg); } - else - emit_move_insn (stack_toc_mem, toc_reg); } if (DEFAULT_ABI == ABI_ELFv2) @@ -37813,10 +37882,12 @@ rs6000_call_aix (rtx value, rtx func_desc, rtx tlsarg, rtx cookie) } else { - /* Direct calls use the TOC: for local calls, the callee will - assume the TOC register is set; for non-local calls, the - PLT stub needs the TOC register. */ - abi_reg = toc_reg; + /* No TOC register needed for calls from PC-relative callers. */ + if (!rs6000_pcrel_p (cfun)) + /* Direct calls use the TOC: for local calls, the callee will + assume the TOC register is set; for non-local calls, the + PLT stub needs the TOC register. */ + abi_reg = toc_reg; func_addr = func; } @@ -37866,7 +37937,9 @@ rs6000_sibcall_aix (rtx value, rtx func_desc, rtx tlsarg, rtx cookie) insn = emit_call_insn (insn); /* Note use of the TOC register. */ - use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, TOC_REGNUM)); + if (!rs6000_pcrel_p (cfun)) + use_reg (&CALL_INSN_FUNCTION_USAGE (insn), + gen_rtx_REG (Pmode, TOC_REGNUM)); } /* Expand code to perform a call under the SYSV4 ABI. */ diff --git a/gcc/config/rs6000/rs6000.md b/gcc/config/rs6000/rs6000.md index 71613e21384..e1d9045c5bb 100644 --- a/gcc/config/rs6000/rs6000.md +++ b/gcc/config/rs6000/rs6000.md @@ -147,6 +147,7 @@ UNSPEC_PLTSEQ UNSPEC_PLT16_HA UNSPEC_PLT16_LO + UNSPEC_PLT_PCREL ]) ;; @@ -10267,6 +10268,20 @@ { return rs6000_pltseq_template (operands, 3); }) + +(define_insn "*pltseq_plt_pcrel" + [(set (match_operand:P 0 "gpc_reg_operand" "=r") + (unspec:P [(match_operand:P 1 "" "") + (match_operand:P 2 "symbol_ref_operand" "s") + (match_operand:P 3 "" "")] + UNSPEC_PLT_PCREL))] + "HAVE_AS_PLTSEQ && TARGET_TLS_MARKERS + && rs6000_pcrel_p (cfun)" +{ + return rs6000_pltseq_template (operands, 4); +} + [(set_attr "type" "load") + (set_attr "length" "12")]) ;; Call and call_value insns ;; For the purposes of expanding calls, Darwin is very similar to SYSV. @@ -10582,7 +10597,11 @@ (match_operand 1)) (clobber (reg:P LR_REGNO))] "DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2" - "bl %z0" +{ + if (rs6000_pcrel_p (cfun)) + return "bl %z0@notoc"; + return "bl %z0"; +} [(set_attr "type" "branch")]) (define_insn "*call_value_local_aix" @@ -10592,7 +10611,11 @@ (clobber (reg:P LR_REGNO))] "(DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2) && !IS_NOMARK_TLSGETADDR (operands[2])" - "bl %z1" +{ + if (rs6000_pcrel_p (cfun)) + return "bl %z1@notoc"; + return "bl %z1"; +} [(set_attr "type" "branch")]) ;; Call to AIX abi function which may be in another module. @@ -10607,7 +10630,10 @@ return rs6000_call_template (operands, 0); } [(set_attr "type" "branch") - (set_attr "length" "8")]) + (set (attr "length") + (if_then_else (match_test "rs6000_pcrel_p (cfun)") + (const_int 4) + (const_int 8)))]) (define_insn "*call_value_nonlocal_aix" [(set (match_operand 0 "" "") @@ -10623,11 +10649,14 @@ } [(set_attr "type" "branch") (set (attr "length") - (if_then_else (match_test "IS_NOMARK_TLSGETADDR (operands[2])") - (if_then_else (match_test "TARGET_CMODEL != CMODEL_SMALL") - (const_int 16) - (const_int 12)) - (const_int 8)))]) + (plus (if_then_else (match_test "IS_NOMARK_TLSGETADDR (operands[2])") + (if_then_else (match_test "TARGET_CMODEL != CMODEL_SMALL") + (const_int 8) + (const_int 4)) + (const_int 0)) + (if_then_else (match_test "rs6000_pcrel_p (cfun)") + (const_int 4) + (const_int 8))))]) ;; Call to indirect functions with the AIX abi using a 3 word descriptor. ;; Operand0 is the addresss of the function to call @@ -10700,6 +10729,21 @@ (const_string "12") (const_string "8")))]) +(define_insn "*call_indirect_pcrel" + [(call (mem:SI (match_operand:P 0 "indirect_call_operand" "c,*l,X")) + (match_operand 1)) + (clobber (reg:P LR_REGNO))] + "rs6000_pcrel_p (cfun)" +{ + return rs6000_indirect_call_template (operands, 0); +} + [(set_attr "type" "jmpreg") + (set (attr "length") + (if_then_else (and (match_test "!rs6000_speculate_indirect_jumps") + (match_test "which_alternative != 1")) + (const_string "8") + (const_string "4")))]) + (define_insn "*call_value_indirect_elfv2" [(set (match_operand 0 "" "") (call (mem:SI (match_operand:P 1 "indirect_call_operand" "c,*l,X")) @@ -10728,6 +10772,31 @@ (const_string "12") (const_string "8"))))]) +(define_insn "*call_value_indirect_pcrel" + [(set (match_operand 0 "" "") + (call (mem:SI (match_operand:P 1 "indirect_call_operand" "c,*l,X")) + (match_operand:P 2 "unspec_tls" ""))) + (clobber (reg:P LR_REGNO))] + "rs6000_pcrel_p (cfun)" +{ + if (IS_NOMARK_TLSGETADDR (operands[2])) + rs6000_output_tlsargs (operands); + + return rs6000_indirect_call_template (operands, 1); +} + [(set_attr "type" "jmpreg") + (set (attr "length") + (plus + (if_then_else (match_test "IS_NOMARK_TLSGETADDR (operands[2])") + (if_then_else (match_test "TARGET_CMODEL != CMODEL_SMALL") + (const_int 8) + (const_int 4)) + (const_int 0)) + (if_then_else (and (match_test "!rs6000_speculate_indirect_jumps") + (match_test "which_alternative != 1")) + (const_string "8") + (const_string "4"))))]) + ;; Call subroutine returning any type. (define_expand "untyped_call" [(parallel [(call (match_operand 0 "") diff --git a/gcc/testsuite/gcc.target/powerpc/notoc-direct-1.c b/gcc/testsuite/gcc.target/powerpc/notoc-direct-1.c new file mode 100644 index 00000000000..c7d322c1c96 --- /dev/null +++ b/gcc/testsuite/gcc.target/powerpc/notoc-direct-1.c @@ -0,0 +1,41 @@ ++/* { dg-do compile } */ ++/* { dg-options "-mdejagnu-cpu=future -O2" } */ ++/* { dg-require-effective-target powerpc_elfv2 } */ + +/* Test that calls generated from PC-relative code are + annotated with @notoc. */ + +extern int yy0 (int); +extern void yy1 (int); + +int zz0 (void) __attribute__((noinline)); +void zz1 (int) __attribute__((noinline)); + +int xx (void) +{ + yy1 (7); + return yy0 (5); +} + +int zz0 () +{ + asm (""); + return 16; +}; + +void zz1 (int a __attribute__((__unused__))) +{ + asm (""); +}; + +int ww (void) +{ + zz1 (zz0 ()); + return 4; +} + +/* { dg-final { scan-assembler {yy1@notoc} } } */ +/* { dg-final { scan-assembler {yy0@notoc} } } */ +/* { dg-final { scan-assembler {zz1@notoc} } } */ +/* { dg-final { scan-assembler {zz0@notoc} } } */ + diff --git a/gcc/testsuite/gcc.target/powerpc/pcrel-sibcall-1.c b/gcc/testsuite/gcc.target/powerpc/pcrel-sibcall-1.c new file mode 100644 index 00000000000..7c767e2ba32 --- /dev/null +++ b/gcc/testsuite/gcc.target/powerpc/pcrel-sibcall-1.c @@ -0,0 +1,46 @@ +/* { dg-do compile } */ +/* { dg-options "-mdejagnu-cpu=future -O2" } */ +/* { dg-require-effective-target powerpc_elfv2 } */ + +/* Test that potential sibcalls are not generated when the caller preserves + the TOC and the callee doesn't, or vice versa. */ + +int x (void) __attribute__((noinline)); +int y (void) __attribute__((noinline)); +int xx (void) __attribute__((noinline)); + +int x (void) +{ + return 1; +} + +int y (void) +{ + return 2; +} + +int sib_call (void) +{ + return x (); +} + +#pragma GCC target ("cpu=power9") +int normal_call (void) +{ + return y (); +} + +int xx (void) +{ + return 1; +} + +#pragma GCC target ("cpu=future") +int notoc_call (void) +{ + return xx (); +} + +/* { dg-final { scan-assembler {\mb x@notoc\M} } } */ +/* { dg-final { scan-assembler {\mbl y\M} } } */ +/* { dg-final { scan-assembler {\mbl xx@notoc\M} } } */