From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by sourceware.org (Postfix) with ESMTPS id B09E0385840C for ; Fri, 8 Oct 2021 14:56:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B09E0385840C Received: by mail-pj1-x1031.google.com with SMTP id qe4-20020a17090b4f8400b0019f663cfcd1so9595655pjb.1 for ; Fri, 08 Oct 2021 07:56:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=v9XVAY8v6LB1j5/b1rgEzan1iy7zHwwivdONIYx9zm8=; b=wb3mbuDVwQQioxRyCx7/T1gEg/57VIDz/RixOY2PK/eQCZzmItDZXsgnnC1nLMky3D b2FUhMNXzBbc6lHAjCBLkMY1VNi2fOO+j+cSG2j9T73YQSOBnq9u2khGKWU/m6GkpDxT oB+fZxvlD6ewHMxoJDQ31nS0voha7aJzLFrIqPnCgUf4mAOSTPNhsXv8k36ccppwmiOL LRJy0xVnDw4FMg0cX2oAJIfLayBL3RSQqPAV1ktN7JqO++tC1fW0lBuNddmvJwCBE9Ge vTCEQtTOfzgnhtGsBuXURG5sTzAHzsD4/iUDkBT+8k3IDUbYFFVBUzqBN0OE8OQ+xH3q SQbA== X-Gm-Message-State: AOAM533CKcDQMATbRuRS7OW2jzceBsHNDIDu1NuZwOxrvcn+IwXSDzS2 lWDI2sk0hbpYPk/VnozBOE1a3Ny4jd0yySxXDRk= X-Google-Smtp-Source: ABdhPJzN+b3bTuh/72cydNNXBvTF0eSs8JrmiUUGMZu3Xr+BY0OT89cs0EBo4KAVX20dl29VsYmKZh4xjw7+/vvuSVY= X-Received: by 2002:a17:902:cec9:b0:13f:17c2:8f14 with SMTP id d9-20020a170902cec900b0013f17c28f14mr2688069plg.66.1633704968573; Fri, 08 Oct 2021 07:56:08 -0700 (PDT) MIME-Version: 1.0 References: <20211004135354.1395400-1-hjl.tools@gmail.com> In-Reply-To: From: "H.J. Lu" Date: Fri, 8 Oct 2021 07:55:32 -0700 Message-ID: Subject: Re: [PATCH] Improve integer bit test on atomic builtin return To: Richard Biener Cc: GCC Patches , Jakub Jelinek Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3024.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 Oct 2021 14:56:15 -0000 On Fri, Oct 8, 2021 at 12:16 AM Richard Biener wrote: > > On Tue, 5 Oct 2021, H.J. Lu wrote: > > > On Tue, Oct 5, 2021 at 3:07 AM Richard Biener wrote: > > > > > > On Mon, 4 Oct 2021, H.J. Lu wrote: > > > > > > > commit adedd5c173388ae505470df152b9cb3947339566 > > > > Author: Jakub Jelinek > > > > Date: Tue May 3 13:37:25 2016 +0200 > > > > > > > > re PR target/49244 (__sync or __atomic builtins will not emit 'lock bts/btr/btc') > > > > > > > > optimized bit test on atomic builtin return with lock bts/btr/btc. But > > > > it works only for unsigned integers since atomic builtins operate on the > > > > 'uintptr_t' type. It fails on bool: > > > > > > > > _1 = atomic builtin; > > > > _4 = (_Bool) _1; > > > > > > > > and signed integers: > > > > > > > > _1 = atomic builtin; > > > > _2 = (int) _1; > > > > _5 = _2 & (1 << N); > > > > > > > > Improve bit test on atomic builtin return by converting: > > > > > > > > _1 = atomic builtin; > > > > _4 = (_Bool) _1; > > > > > > > > to > > > > > > > > _1 = atomic builtin; > > > > _5 = _1 & (1 << 0); > > > > _4 = (_Bool) _5; > > > > > > > > and converting: > > > > > > > > _1 = atomic builtin; > > > > _2 = (int) _1; > > > > _5 = _2 & (1 << N); > > > > > > > > to > > > > _1 = atomic builtin; > > > > _6 = _1 & (1 << N); > > > > _5 = (int) _6; > > > > > > Why not do this last bit with match.pd patterns (and independent on > > > whether _1 is defined by an atomic builtin)? For the first suggested > > > > The full picture is > > > > _1 = _atomic_fetch_or_* (ptr_6, mask, _3); > > _2 = (int) _1; > > _5 = _2 & mask; > > > > to > > > > _1 = _atomic_fetch_or_* (ptr_6, mask, _3); > > _6 = _1 & mask; > > _5 = (int) _6; > > > > It is useful only if 2 masks are the same. > > > > > transform that's likely going to be undone by folding, no? > > > > > > > The bool case is > > > > _1 = __atomic_fetch_or_* (ptr_6, 1, _3); > > _4 = (_Bool) _1; > > > > to > > > > _1 = __atomic_fetch_or_* (ptr_6, 1, _3); > > _5 = _1 & 1; > > _4 = (_Bool) _5; > > > > Without __atomic_fetch_or_*, the conversion isn't needed. > > After the conversion, optimize_atomic_bit_test_and will > > immediately optimize the code sequence to > > > > _6 = .ATOMIC_BIT_TEST_AND_SET (&v, 0, 0, 0); > > _4 = (_Bool) _6; > > > > and there is nothing to fold after it. > > Hmm, I see - so how about instead teaching the code that > produces the .ATOMIC_BIT_TEST_AND_SET the alternate forms instead > of doing the intermediate step separately? > The old algorithm is 1. Check gimple forms. Return if the form isn't supported. 2. Do transformation. My current approach treats the gimple forms accepted by the old algorithm as canonical forms and changes the algorithm to 1. If gimple forms aren't canonical, then a. If gimple forms can't be transformed to canonical forms, return; b. Transform to canonical form. endif 2. Check gimple forms. Return if the form isn't supported. 3. Do transformation. The #2 check is redundant when gimple forms have been transformed to canonical forms. I can change my patch to 1. If gimple forms aren't canonical, then a. If gimple forms can't be transformed to canonical forms, return; b. Transform to canonical form. else Check gimple forms. Return if the form isn't supported. endif 2. Do transformation. The advantage of canonical forms is that we don't have to transform all different forms. Does it sound OK? Thanks. -- H.J.