From patchwork Fri Aug 3 07:24:00 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Uros Bizjak X-Patchwork-Id: 174903 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 F3D202C0095 for ; Fri, 3 Aug 2012 17:24:26 +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=1344583467; h=Comment: DomainKey-Signature:Received:Received:Received:Received: MIME-Version:Received:Received:In-Reply-To:References:Date: Message-ID:Subject:From:To:Cc:Content-Type:Mailing-List: Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:Sender:Delivered-To; bh=NV9SmOa+drlERqiankuw1P4J56k=; b=poE2Xnwf3xmEWuHya5BSJ/8iZJmFX1W1EHFpBjaCd2Ncs77UimQTQTlOMApEPW FjkrqRGQ7rLH9dphvHhQ0jinxVdCtEyuNIjXXgB/hM05G3v2dSP5TCt4E6dwWcHq hXiLFiMCPZALkfRJSr4fV86zcrAQWkbc8jTGGvtjHK6ZM= 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:MIME-Version:Received:Received:In-Reply-To:References:Date:Message-ID:Subject:From:To:Cc:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=TvuM5X1f7x/IanWzWroH7oAUanjiDcbApwQIOqJGxBRi0+3WzwsuC1NfIJL3hw 7XOcVn2XRoNGiCng6EAefR/E85lgNkVeBZ7LcMUyopnRAdzE+mTyjmm6nJBSaZDb IB+8k8/nQdQDiKHZKlIBfRgTf9Pz5I+cFpZM9XdS494vU=; Received: (qmail 12957 invoked by alias); 3 Aug 2012 07:24:21 -0000 Received: (qmail 12948 invoked by uid 22791); 3 Aug 2012 07:24:18 -0000 X-SWARE-Spam-Status: No, hits=-5.0 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, KHOP_RCVD_TRUST, KHOP_THREADED, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_NO, RCVD_IN_HOSTKARMA_YE, TW_ZJ X-Spam-Check-By: sourceware.org Received: from mail-yx0-f175.google.com (HELO mail-yx0-f175.google.com) (209.85.213.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 03 Aug 2012 07:24:01 +0000 Received: by yenl13 with SMTP id l13so479241yen.20 for ; Fri, 03 Aug 2012 00:24:00 -0700 (PDT) MIME-Version: 1.0 Received: by 10.66.88.131 with SMTP id bg3mr144819pab.16.1343978640660; Fri, 03 Aug 2012 00:24:00 -0700 (PDT) Received: by 10.66.11.130 with HTTP; Fri, 3 Aug 2012 00:24:00 -0700 (PDT) In-Reply-To: References: Date: Fri, 3 Aug 2012 09:24:00 +0200 Message-ID: Subject: Re: [PATCH, i386]: Implement atomic_fetch_sub From: Uros Bizjak To: gcc-patches@gcc.gnu.org Cc: Richard Henderson 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 Fri, Aug 3, 2012 at 8:40 AM, Uros Bizjak wrote: > We can implement atomic_fetch_sub with atomic_fetch_add and inverted > operand 2. However, we have to prevent overflows with negation, so > only const_int operand 2 is allowed in the expander. > > 2012-08-02 Uros Bizjak > > PR target/54087 > * config/i386/sync.md (atomic_fetch_sub): New expander. > > Tested on x86_64-pc-linux-gnu {,-m32}. > > I will wait for a day for possible comments. Patch attached again, this time with a testcase. 2012-08-02 Uros Bizjak PR target/54087 * gcc.target/i386/pr54087.c: New test. Uros. Index: config/i386/sync.md =================================================================== --- config/i386/sync.md (revision 190111) +++ config/i386/sync.md (working copy) @@ -520,6 +520,31 @@ return "lock{%;} add{}\t{%1, %0|%0, %1}"; }) +;; We can use atomic_fetch_add with negated operand 2 to implement +;; atomic_fetch_sub. We have to prevent overflows with negation, so +;; only const_int operand 2 is allowed. +(define_expand "atomic_fetch_sub" + [(set (match_operand:SWI 0 "register_operand") + (unspec_volatile:SWI + [(match_operand:SWI 1 "memory_operand") + (match_operand:SI 3 "const_int_operand")] ;; model + UNSPECV_XCHG)) + (set (match_dup 1) + (minus:SWI (match_dup 1) + (match_operand:SWI 2 "const_int_operand"))) + (clobber (reg:CC FLAGS_REG))] + "TARGET_XADD" +{ + /* Avoid overflows. */ + if (mode_signbit_p (mode, operands[2])) + FAIL; + + emit_insn (gen_atomic_fetch_add (operands[0], operands[1], + negate_rtx (mode, operands[2]), + operands[3])); + DONE; +}) + ;; Recall that xchg implicitly sets LOCK#, so adding it again wastes space. ;; In addition, it is always a full barrier, so we can ignore the memory model. (define_insn "atomic_exchange" Index: testsuite/gcc.target/i386/pr54087.c =================================================================== --- testsuite/gcc.target/i386/pr54087.c (revision 0) +++ testsuite/gcc.target/i386/pr54087.c (revision 0) @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ +/* { dg-additional-options "-march=i486" { target ia32 } } */ + +int v; + +int foo (void) +{ + return __atomic_sub_fetch (&v, 5, __ATOMIC_SEQ_CST); +} + +/* { dg-final { scan-assembler "xadd" } } */ +/* { dg-final { scan-assembler-not "cmpxchg" } } */