From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) by sourceware.org (Postfix) with ESMTPS id 6BB193858029 for ; Tue, 2 Aug 2022 17:22:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6BB193858029 Received: by mail-qt1-x82e.google.com with SMTP id bz13so10814946qtb.7 for ; Tue, 02 Aug 2022 10:22:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=6U4XdHENzU/FtKzr8N9471QmliRYOUOjPqYNjl/rBzk=; b=Jkwp7VQ28jHRYMv5nt1Sw9BiEtSfLoD/+9P4XRP6ymWOW+d7du7+NdBpTwHTGPlRU/ QVPY9erQu1QCXQTQZIzXpDsABwNFW4FAsDAv6ltOftwHETa6Xpp8NZPb8bE971BRFWlf gBvPbO9B9ShVpBWI/FinJ+SBEOMYR843pr1HXwY19YL1LLkZH+aPIUbzxGGBMp6VyTFr +3JctO3tj+5EgDfXR/PwLs1VLZeQFX5vNJ9992kJVZV6axhfTPM+bTeT7ODKQFRFQ/9D 0eqm4/N+o4OBL3N7L7e3xABisgjNTXtv+K2OfIm3ewRK2WzR4+EK8S+X1Nu38yTaBo+4 f9Nw== X-Gm-Message-State: AJIora/Lmek7nEd64I9wn0Ao4etHONTq6H2o1IfsDg1s1BsfFJ91E0Tm nfdbJ7fnXNEBm/pK1c7i8/b6XmgVEpxbsGkYZhmAJzOOtuY= X-Google-Smtp-Source: AGRyM1tBqCrf5BuMLG0iqHC0IEYMpKMJo8k0io2ZeI+bR6gLNHWnbtEoPnocGt57uZ3D8ri1DbiFi4E584VKYUtb1kg= X-Received: by 2002:ac8:5905:0:b0:31e:e346:e080 with SMTP id 5-20020ac85905000000b0031ee346e080mr19118892qty.569.1659460972514; Tue, 02 Aug 2022 10:22:52 -0700 (PDT) MIME-Version: 1.0 References: <032901d8a2cf$fc07cfd0$f4176f70$@nextmovesoftware.com> <007f01d8a691$a3b95310$eb2bf930$@nextmovesoftware.com> In-Reply-To: <007f01d8a691$a3b95310$eb2bf930$@nextmovesoftware.com> From: Uros Bizjak Date: Tue, 2 Aug 2022 19:22:42 +0200 Message-ID: Subject: Re: [x86 PATCH] Support logical shifts by (some) integer constants in TImode STV. To: Roger Sayle Cc: gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Tue, 02 Aug 2022 17:22:55 -0000 On Tue, Aug 2, 2022 at 7:02 PM Roger Sayle wrote: > > > Hi Uros, > > > From: Uros Bizjak > > Sent: 31 July 2022 18:23 > > To: Roger Sayle > > On Fri, Jul 29, 2022 at 12:18 AM Roger Sayle > > wrote: > > > > > > This patch improves TImode STV by adding support for logical shifts by > > > integer constants that are multiples of 8. For the test case: > > > > > > __int128 a, b; > > > void foo() { a = b << 16; } > > > > > > on x86_64, gcc -O2 currently generates: > > > > > > movq b(%rip), %rax > > > movq b+8(%rip), %rdx > > > shldq $16, %rax, %rdx > > > salq $16, %rax > > > movq %rax, a(%rip) > > > movq %rdx, a+8(%rip) > > > ret > > > > > > with this patch we now generate: > > > > > > movdqa b(%rip), %xmm0 > > > pslldq $2, %xmm0 > > > movaps %xmm0, a(%rip) > > > ret > > > > > > This patch has been tested on x86_64-pc-linux-gnu with make bootstrap > > > and make -k check. both with and without --target_board=unix{-m32}, > > > with no new failures. Ok for mainline? > > > > > > > > > 2022-07-28 Roger Sayle > > > > > > gcc/ChangeLog > > > * config/i386/i386-features.cc (compute_convert_gain): Add gain > > > for converting suitable TImode shift to a V1TImode shift. > > > (timode_scalar_chain::convert_insn): Add support for converting > > > suitable ASHIFT and LSHIFTRT. > > > (timode_scalar_to_vector_candidate_p): Consider logical shifts > > > by integer constants that are multiples of 8 to be candidates. > > > > > > gcc/testsuite/ChangeLog > > > * gcc.target/i386/sse4_1-stv-7.c: New test case. > > > > + case ASHIFT: > > + case LSHIFTRT: > > + /* For logical shifts by constant multiples of 8. */ igain = > > + optimize_insn_for_size_p () ? COSTS_N_BYTES (4) > > + : COSTS_N_INSNS (1); > > > > Isn't the conversion an universal win for -O2 as well as for -Os? The conversion > > to/from XMM register is already accounted for, so for -Os substituting > > shldq/salq with pslldq should always be a win. I'd expect the cost calculation to > > be similar to the general_scalar_chain::compute_convert_gain cost calculation > > with m = 2. > > I agree that the terminology is perhaps a little confusing. The > compute_convert_gain function calculates the total "gain" from an > STV chain, summing the igain of each instruction, and performs > the STV transformation if this total gain is greater than zero. > Hence positive values are good and negative values are bad. > > In this case, of a logical shift by multiple of 8, converting the chain is indeed always > beneficial, reducing by 4 bytes in size when optimizing for size, and avoiding 1 fast > instruction when optimizing for speed. Having a "positive gain of four bytes" sounds bad, > but in this sense the gain is used as a synonym of "benefit" not "magnitude". > > By comparison, shifting by a single bit 128 bit value is always a net loss, requiring > three addition fast instructions, or 15 extra bytes in size. However, it's still worth > considering/capturing these counter-productive (i.e. negative) values, as they > might be compensated for by other wins in the chain. > > Dealing with COSTS_N_BYTES (when optimizing for size) and COSTS_N_INSNS > (when optimizing for speed) allows much finer granularity. For example, > the constant number of bits used in a shift/rotate, or the value of an > immediate constant in a compare have significant effects on the size/speed > of scalar vs. vector code, and this isn't (yet) something easily handled by the > simple "m" approximation used in general_scalar_chain::compute_convert_gain. > > See (comment #5 of) PR target/105034 which mentions the need for more > accurate parameterization of compute_convert_gain (in response to the > undesirable suggestion of simply disabling STV when optimizing for size). > > I hope this explains the above idiom. Hopefully, things will become clearer > when support for shifts by other bit counts, and arithmetic shifts, are added > to this part of the code (STV). I'll be sure to add more comments. Thanks for the explanation, I was confused by the differences between 32bit DImode and 64bit TImode costs (these should IMO be quite similar, so if the TImode cost function is now more precise, DImode cost function should follow it). > [Ok for mainline?] OK. The cost functions are always in need of more accuracy.. Thanks, Uros.