From patchwork Thu Aug 23 15:59:38 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew MacLeod X-Patchwork-Id: 179675 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]) by ozlabs.org (Postfix) with SMTP id 9AD962C013E for ; Fri, 24 Aug 2012 02:00:21 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1346342422; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Message-ID:Date:From:User-Agent:MIME-Version:To:CC:Subject: References:In-Reply-To:Content-Type:Mailing-List:Precedence: List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=ft0VWD/SOfD/kO/L68WOseBLZ9w=; b=UIcNGnVETNvn50U B4jqmoIfAyl0q37HJ1/zV+9KQ/wJI/ZlzPoo023Y4WpoEZHnfuok4QekJQQOXTiZ taFG1sixZWO4zApptt1x1PKmJyYjU0dZ6JK4+bOECfMWIpfwjWjMvVKLz0xjxBWj m3mYDEimwHpxJ6/WQyJTO9+v83KI= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Message-ID:Date:From:User-Agent:MIME-Version:To:CC:Subject:References:In-Reply-To:Content-Type:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=O800otc7fdEpnP7/oubhFszBUEUPVo6z1VMg8j39pcFpopXsCpu+FaoEF4yV6V aE+jrKG69XyhTdRgDQVVwGdCXYLIULIGONUFJcsqnVC7E8/JXIGtBwEhhV4qgG0M 9YMOxZJhV5m+hmcmo6zRz4lOp0k2FQR3nzMP5OqnyQmxg=; Received: (qmail 23365 invoked by alias); 23 Aug 2012 16:00:14 -0000 Received: (qmail 23262 invoked by uid 22791); 23 Aug 2012 16:00:06 -0000 X-SWARE-Spam-Status: No, hits=-7.0 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, KHOP_THREADED, RCVD_IN_DNSWL_HI, RCVD_IN_HOSTKARMA_W, RP_MATCHES_RCVD, SPF_HELO_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 23 Aug 2012 15:59:41 +0000 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q7NFxeZC020125 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 23 Aug 2012 11:59:40 -0400 Received: from [10.11.11.97] (vpn-11-97.rdu.redhat.com [10.11.11.97]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q7NFxdOq029692; Thu, 23 Aug 2012 11:59:39 -0400 Message-ID: <5036536A.3000208@redhat.com> Date: Thu, 23 Aug 2012 11:59:38 -0400 From: Andrew MacLeod User-Agent: Mozilla/5.0 (X11; Linux i686; rv:14.0) Gecko/20120717 Thunderbird/14.0 MIME-Version: 1.0 To: Uros Bizjak CC: Richard Henderson , Richard Guenther , gcc-patches@gcc.gnu.org Subject: Re: [PATCH, i386]: Implement atomic_fetch_sub References: <501BE2D1.5060709@redhat.com> <501BE968.5030609@redhat.com> <501C1468.5090604@redhat.com> <501C1DFD.1080607@redhat.com> In-Reply-To: <501C1DFD.1080607@redhat.com> X-IsSubscribed: yes 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 On 08/03/2012 02:52 PM, Andrew MacLeod wrote: > > I'll take a look at it next week unless someone gets to it first. > > Andrew OK, so maybe I sort of forgot about implementing the generic transformation until now. This patch bootstraps and passes all the testsuite cases. Factored out a little code to make sure we only generate the add if there is a direct opcode for it. Also implemented the reverse since it was trivial... if an atomic_add doesn't generate anything, it'll try an atomic_sub to see if thats possible. If that all fails, then we continue to regress to a compare and swap loop. bootstraps with no new regressions and verified to fix the PR with no side-effects OK for mainline? Andrew 2012-08-23 Andrew MacLeod gcc PR target/54087 * optabs.c (expand_atomic_fetch_op_no_fallback): New. Factored code from expand_atomic_fetch_op. (expand_atomic_fetch_op): iTry atomic_{add|sub} operations in terms of the other one if direct opcode fails. testsuite * gcc.dg/pr54087.c: New testcase for atomic_sub -> atomic_add when atomic_sub fails. Index: optabs.c =================================================================== *** optabs.c (revision 190595) --- optabs.c (working copy) *************** maybe_emit_op (const struct atomic_op_fu *** 7745,7754 **** CODE is the operation being performed (OP) MEMMODEL is the memory model variant to use. AFTER is true to return the result of the operation (OP_fetch). ! AFTER is false to return the value before the operation (fetch_OP). */ ! rtx ! expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code, ! enum memmodel model, bool after) { enum machine_mode mode = GET_MODE (mem); struct atomic_op_functions optab; --- 7745,7759 ---- CODE is the operation being performed (OP) MEMMODEL is the memory model variant to use. AFTER is true to return the result of the operation (OP_fetch). ! AFTER is false to return the value before the operation (fetch_OP). ! ! This function will *only* generate instructions if there is a direct ! optab. No compare and swap loops or libcalls will be generated. */ ! ! static rtx ! expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val, ! enum rtx_code code, enum memmodel model, ! bool after) { enum machine_mode mode = GET_MODE (mem); struct atomic_op_functions optab; *************** expand_atomic_fetch_op (rtx target, rtx *** 7821,7833 **** --- 7826,7891 ---- } } + /* No direct opcode can be generated. */ + return NULL_RTX; + } + + + + /* This function expands an atomic fetch_OP or OP_fetch operation: + TARGET is an option place to stick the return value. const0_rtx indicates + the result is unused. + atomically fetch MEM, perform the operation with VAL and return it to MEM. + CODE is the operation being performed (OP) + MEMMODEL is the memory model variant to use. + AFTER is true to return the result of the operation (OP_fetch). + AFTER is false to return the value before the operation (fetch_OP). */ + rtx + expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code, + enum memmodel model, bool after) + { + enum machine_mode mode = GET_MODE (mem); + rtx result; + bool unused_result = (target == const0_rtx); + + result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model, + after); + + if (result) + return result; + + /* Add/sub can be implemented by doing the reverse operation with -(val). */ + if (code == PLUS || code == MINUS) + { + rtx tmp; + enum rtx_code reverse = (code == PLUS ? MINUS : PLUS); + + start_sequence (); + tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true); + result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse, + model, after); + if (result) + { + /* PLUS worked so emit the insns and return. */ + tmp = get_insns (); + end_sequence (); + emit_insn (tmp); + return result; + } + + /* PLUS did not work, so throw away the negation code and continue. */ + end_sequence (); + } + /* Try the __sync libcalls only if we can't do compare-and-swap inline. */ if (!can_compare_and_swap_p (mode, false)) { rtx libfunc; bool fixup = false; enum rtx_code orig_code = code; + struct atomic_op_functions optab; + get_atomic_op_for_code (&optab, code); libfunc = optab_libfunc (after ? optab.fetch_after : optab.fetch_before, mode); if (libfunc == NULL Index: testsuite/gcc.dg/pr54087.c =================================================================== *** testsuite/gcc.dg/pr54087.c (revision 0) --- testsuite/gcc.dg/pr54087.c (revision 0) *************** *** 0 **** --- 1,18 ---- + /* PR54087. Verify __atomic_sub (val) uses __atomic_add (-val) if there is no + atomic_aub. */ + /* { dg-require-effective-target sync_int_long } */ + /* { dg-do compile { target { i?86-*-* x86_64-*-* } } } */ + /* { dg-final { scan-assembler-times "xadd" 2 } } */ + + + int a; + + int f1(int p) + { + return __atomic_sub_fetch(&a, p, __ATOMIC_SEQ_CST) == 0; + } + + int f2(int p) + { + return __atomic_fetch_sub(&a, p, __ATOMIC_SEQ_CST) - p == 0; + }