From patchwork Mon May 11 16:11:18 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Monakov X-Patchwork-Id: 470932 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 0CFF1140157 for ; Tue, 12 May 2015 02:11:31 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=UU0YxEej; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:in-reply-to:message-id:references :mime-version:content-type; q=dns; s=default; b=yF3U6odnkDWjQxv7 J6nT+pJbwUxI6TUbZQrHOridl/K8fCpcMCi6KTp6/DQzsOorwfNUwcj+SocZy2zG PABPqtx2TeNOM8AA0r39pyke9+zUKzwHJZptFM3L9eZPT2AixVT2SPKLRyPmdmxp OncYhOeUn/pRoio6Y7tPqPmGODA= 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:date :from:to:cc:subject:in-reply-to:message-id:references :mime-version:content-type; s=default; bh=hiEjIlvSPj1nMo7GrxFt21 MWk5c=; b=UU0YxEejmRPKOVHftymYg+utBWPVtYe1ZHiiq27rXPpdudd8ySwhai YVAUQsOZF193Y64h7S1xnQC60v08QjknwL+LhWFST9Gv/9o2HiA9q0kqvzVm4UHw NzpQFv5ci4rkETP+lR6+UMzX2VERSUqScEk/x4Hzae0q5X0M3b0vM= Received: (qmail 110219 invoked by alias); 11 May 2015 16:11:23 -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 108964 invoked by uid 89); 11 May 2015 16:11:23 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.1 required=5.0 tests=AWL, BAYES_00, KAM_ASCII_DIVIDERS, KAM_LAZY_DOMAIN_SECURITY, T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: smtp.ispras.ru Received: from smtp.ispras.ru (HELO smtp.ispras.ru) (83.149.199.79) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 11 May 2015 16:11:21 +0000 Received: from [10.10.3.121] (unknown [83.149.199.91]) by smtp.ispras.ru (Postfix) with ESMTP id DD448214EC; Mon, 11 May 2015 19:11:18 +0300 (MSK) Date: Mon, 11 May 2015 19:11:18 +0300 (MSK) From: Alexander Monakov To: Jan Hubicka cc: gcc-patches@gcc.gnu.org, Rich Felker Subject: Re: [PATCH i386] PR65753: allow PIC tail calls via function pointers In-Reply-To: <20150510163753.GF9659@atrey.karlin.mff.cuni.cz> Message-ID: References: <1430757479-14241-1-git-send-email-amonakov@ispras.ru> <1430757479-14241-2-git-send-email-amonakov@ispras.ru> <20150510163753.GF9659@atrey.karlin.mff.cuni.cz> User-Agent: Alpine 2.11 (LNX 23 2013-08-11) MIME-Version: 1.0 On Sun, 10 May 2015, Jan Hubicka wrote: > You probably need to update comment here. I wonder what happens when we optimize > indirect call to direct call to global function at RTL level? I suppose we are > safe here, because at RTL level we explicitly represent if we refer to PLT entry > or the functionaddress itself and we never optimize one to the other? > > Patch is OK if you make sure that this works and update the comment. I think we are safe: to have things break we'd have to have a GOT-relative memory load be combined with a branch on the RTL level, and GOT loads have UNSPEC_GOT. I have used the following example to try to induce failure: void foo(void); void bar() { void (*p)(void) = foo; p(); } With the following options: gcc -fPIC -m32 -O -foptimize-sibling-calls -fno-tree-ccp -fno-tree-copy-prop -fno-tree-fre -fno-tree-dominator-opts -fno-tree-ter GCC has indirect call after pass_expand. Without -fPIC it is transformed into direct call in pass_combine, with -fPIC it is kept as is. I've added a testcase. Below is what I'm checking in. Thanks! Index: testsuite/gcc.target/i386/pr65753.c =================================================================== --- testsuite/gcc.target/i386/pr65753.c (revision 0) +++ testsuite/gcc.target/i386/pr65753.c (revision 0) @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-fPIC -O" } */ +/* { dg-final { scan-assembler-not "call" } } */ + +void foo(void (*bar)(void)) +{ + bar(); +} Index: config/i386/i386.c =================================================================== --- config/i386/i386.c (revision 223002) +++ config/i386/i386.c (working copy) @@ -5473,12 +5473,12 @@ rtx a, b; /* If we are generating position-independent code, we cannot sibcall - optimize any indirect call, or a direct call to a global function, - as the PLT requires %ebx be live. (Darwin does not have a PLT.) */ + optimize direct calls to global functions, as the PLT requires + %ebx be live. (Darwin does not have a PLT.) */ if (!TARGET_MACHO && !TARGET_64BIT && flag_pic - && (!decl || !targetm.binds_local_p (decl))) + && decl && !targetm.binds_local_p (decl)) return false;