From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by sourceware.org (Postfix) with ESMTPS id 0E48C38418BB for ; Wed, 1 Jun 2022 15:00:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0E48C38418BB Received: by mail-pl1-x631.google.com with SMTP id c2so2068021plh.2 for ; Wed, 01 Jun 2022 08:00:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=FGmsd+u9hck2hIFj4n0kaWlKGiyZwBQfrwmpPXV09SY=; b=P6az5Yqupt0n20ZwjUjfFbqTj+8M0xf+LkCFQDoohwponElurWwIfuLpeFO2IToB9U TH7Kq8iFzTg8CCM78eHly1s9bPlj8tLcVE7XnZ0rodERTGvMA6DPyToYriX/pWmtsGZZ oka3VdIxL7mEjZN4fTDz7zpkAePZdjZno/Hhc1ZcrcURLsaInQeG0sgF5ErRCYld0Mp5 KOvgdrn9Z2f1u3l8ZHZJYfApdH1QOvxd+HhoIaiVK9useP633gmMWpUoOHLKtnvx2RBj TXTo7GvXI/C0rK6LL35HiPlOtgbNzg9G3jzCIaLC6lO5o/CkVvtkqeGXAChKODkUQkj4 kqFg== X-Gm-Message-State: AOAM533xOH8ib+fdY2aNy1Zr0SEheveZVHIHGaSA78KLMqmkIdNr3rWC 7z2RUgm8Q1X3Jk3txEymT/vsXxda69RuDg== X-Google-Smtp-Source: ABdhPJx63po+1rg9Uzjns9XRtZl88ElmjfJWoCT5sTiEWk8TJN2oeiCf8lImv462Ziw7eIivvuwEcg== X-Received: by 2002:a17:90b:1e06:b0:1e3:7d5:b542 with SMTP id pg6-20020a17090b1e0600b001e307d5b542mr15670719pjb.230.1654095646361; Wed, 01 Jun 2022 08:00:46 -0700 (PDT) Received: from [172.31.0.204] (c-73-63-24-84.hsd1.ut.comcast.net. [73.63.24.84]) by smtp.gmail.com with ESMTPSA id u79-20020a627952000000b0051ba7515e0dsm1639068pfc.54.2022.06.01.08.00.45 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 01 Jun 2022 08:00:45 -0700 (PDT) Message-ID: Date: Wed, 1 Jun 2022 09:00:44 -0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: [PATCH] Fold truncations of left shifts in match.pd Content-Language: en-US To: gcc-patches@gcc.gnu.org References: <020201d87420$1e944a80$5bbcdf80$@nextmovesoftware.com> From: Jeff Law In-Reply-To: <020201d87420$1e944a80$5bbcdf80$@nextmovesoftware.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: Wed, 01 Jun 2022 15:00:50 -0000 On 5/30/2022 6:23 AM, Roger Sayle wrote: > Whilst investigating PR 55278, I noticed that the tree-ssa optimizers > aren't eliminating the promotions of shifts to "int" as inserted by the > c-family front-ends, instead leaving this simplification to be left to > the RTL optimizers. This patch allows match.pd to do this itself earlier, > narrowing (T)(X << C) to (T)X << C when the constant C is known to be > valid for the (narrower) type T. > > Hence for this simple test case: > short foo(short x) { return x << 5; } > > the .optimized dump currently looks like: > > short int foo (short int x) > { > int _1; > int _2; > short int _4; > > [local count: 1073741824]: > _1 = (int) x_3(D); > _2 = _1 << 5; > _4 = (short int) _2; > return _4; > } > > but with this patch, now becomes: > > short int foo (short int x) > { > short int _2; > > [local count: 1073741824]: > _2 = x_1(D) << 5; > return _2; > } > > This is always reasonable as RTL expansion knows how to use > widening optabs if it makes sense at the RTL level to perform > this shift in a wider mode. > > Of course, there's often a catch. The above simplification not only > reduces the number of statements in gimple, but also allows further > optimizations, for example including the perception of rotate idioms > and bswap16. Alas, optimizing things earlier than anticipated > requires several testsuite changes [though all these tests have > been confirmed to generate identical assembly code on x86_64]. > The only significant change is that the vectorization pass previously > wouldn't vectorize rotations if the backend doesn't explicitly provide > an optab for them. This is curious as if the rotate is expressed as > ior(lshift,rshift) it will vectorize, and likewise RTL expansion will > generate the iorv(lshiftv,rshiftv) sequence if required for a vector > mode rotation. Hence this patch includes a tweak to the optabs > test in tree-vect-stmts.cc's vectorizable_shifts to better reflect > the functionality supported by RTL expansion. > > 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-05-30 Roger Sayle > > gcc/ChangeLog > * match.pd (convert (lshift @1 INTEGER_CST@2)): Narrow integer > left shifts by a constant when the result is truncated, and the > shift constant is well-defined for the narrower mode. > * tree-vect-stmts.cc (vectorizable_shift): Rotations by > constants are vectorizable, if the backend supports logical > shifts and IOR logical operations in the required vector mode. > > gcc/testsuite/ChangeLog > * gcc.dg/fold-convlshift-4.c: New test case. > * gcc.dg/optimize-bswaphi-1.c: Update found bswap count. > * gcc.dg/tree-ssa/pr61839_3.c: Shift is now optimized before VRP. > * gcc.dg/vect/vect-over-widen-1-big-array.c: Remove obsolete tests. > * gcc.dg/vect/vect-over-widen-1.c: Likewise. > * gcc.dg/vect/vect-over-widen-3-big-array.c: Likewise. > * gcc.dg/vect/vect-over-widen-3.c: Likewise. > * gcc.dg/vect/vect-over-widen-4-big-array.c: Likewise. > * gcc.dg/vect/vect-over-widen-4.c: Likewise. So the worry here would be stuff like narrowing the source operand leading to partial stalls.  But as you indicated, if the target really wants to do the shift in a wider mode, it can.  Furthermore, the place to make that decision is at the gimple->rtl border, IMHO. OK. jeff ps.  There may still be another old BZ for the lack of narrowing inhibiting vectorization IIRC.  I don't recall the specifics enough to hazard a guess if this patch will help or not.