From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by sourceware.org (Postfix) with ESMTPS id 81C7C3857012 for ; Mon, 7 Jun 2021 08:25:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 81C7C3857012 Received: by mail-ed1-x531.google.com with SMTP id d13so5738508edt.5 for ; Mon, 07 Jun 2021 01:25:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=ddODOFstMIrfvYRMCgEwuV6jBb9LA4BQanidpqDCoZA=; b=n4ihDuedQvLEphoB/tFdjNxURs4qEWfInuhj/vinmwv6GV03MmPo/ChhiiB3Q50ku6 SG5PG2KeSYVZZjSdffeag/zBLMEWBh5tQMRA0s/k/uNKUyoJCDpIrtxzXcqiolU4Z4rb UXurHmthaA8RCX3cs/zvdHWnblx/yOzmbKpl7kzuP4AuLLY8nHUsVuxHJGrRAcuXJOaf gGTrwNmTtOe+11sFRSXwXDoScBrrLoTdc+gxR4IKD56LGMGgrpmCkPz16xTzqH+KXECQ LRuUZNW74f268amub06PSt2Pijse4Ed9Ee5mUMomC0+6Dyo1UAiUqWvsftVw9IVybDYJ 3Fhw== X-Gm-Message-State: AOAM532Ne/7k75M6XGDiNzR97Nul5KxhulHoNs1FU9h3Q9ij/+dmZi0o 5wjKK64LhrPorHWP4xc6pXV7AxCNtUnX0llE1FE= X-Google-Smtp-Source: ABdhPJzsLu7rd+oWojN9szhQr5BseLWU6fkz0cxJo1A1Y5LLFEi99Ptrs0bhlEs7xRgtM3Z1tIBoTHyxB3yHigwYQr4= X-Received: by 2002:aa7:dd81:: with SMTP id g1mr18650342edv.274.1623054343897; Mon, 07 Jun 2021 01:25:43 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Richard Biener Date: Mon, 7 Jun 2021 10:25:33 +0200 Message-ID: Subject: Re: [EXTERNAL] Re: [PATCH] tree-optimization: Optimize division followed by multiply [PR95176] To: Victor Tong Cc: "gcc-patches@gcc.gnu.org" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.8 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.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Mon, 07 Jun 2021 08:25:56 -0000 On Wed, Jun 2, 2021 at 10:55 PM Victor Tong wrote: > > Hi Richard, > > Thanks for reviewing my patch. I did a search online and you're right -- = there isn't a vector modulo instruction. I'll remove the X * (Y / X) --> Y = - (Y % X) pattern and the existing X - (X / Y) * Y --> X % Y from triggerin= g on vector types. > > I looked into why the following pattern isn't triggering: > > (simplify > (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1))) > (view_convert @1)) > > The nop_converts expand into tree_nop_conversion_p checks. In fn2() of th= e testsuite/gcc.dg/fold-minus-6.c, the expression during generic matching l= ooks like: > > 42 - (long int) (42 - 42 % x) > > When looking at the right-hand side of the expression (the (long int) (42= - 42 % x)), the tree_nop_conversion_p check fails because of the type prec= ision difference. The expression inside of the cast has a 32-bit precision = and the outer expression has a 64-bit precision. > > I looked around at other patterns and it seems like nop_convert and view_= convert are used because of underflow/overflow concerns. I'm not familiar w= ith the two constructs. What's the difference between using them and checki= ng TYPE_OVERFLOW_UNDEFINED? In the scenario above, since TYPE_OVERFLOW_UNDE= FINED is true, the second pattern that I added (X - (X - Y) --> Y) gets tri= ggered. But TYPE_OVERFLOW_UNDEFINED is not a good condition here since the conversion is the problematic one and conversions have implementation defined behavior. Now, the above does not match because it wasn't designed to, and for non-constant '42' it would have needed a (convert ...) around the first @0 as well (matching of constants is by value, not by value + type). That said, your +/* X - (X - Y) --> Y */ +(simplify + (minus (convert1? @0) (convert2? (minus @@0 @1))) + (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) && TYPE_OVERFLOW_UNDEFINED(type)) + (convert @1))) would match (int)x - (int)(x - y) where you assert the outer subtract has undefined behavior on overflow but the inner subtract could wrap and the (int) conversion can be truncating or widening. Is that really always a valid transform then? Richard. > Thanks, > Victor > > > From: Richard Biener > Sent: Tuesday, April 27, 2021 1:29 AM > To: Victor Tong > Cc: gcc-patches@gcc.gnu.org > Subject: [EXTERNAL] Re: [PATCH] tree-optimization: Optimize division foll= owed by multiply [PR95176] > > On Thu, Apr 1, 2021 at 1:03 AM Victor Tong via Gcc-patches > wrote: > > > > Hello, > > > > This patch fixes PR tree-optimization/95176. A new pattern in match.pd = was added to transform "a * (b / a)" --> "b - (b % a)". A new test case was= also added to cover this scenario. > > > > The new pattern interfered with the existing pattern of "X - (X / Y) * = Y". In some cases (such as in fn4() in gcc/testsuite/gcc.dg/fold-minus-6.c)= , the new pattern is applied causing the existing pattern to no longer appl= y. This results in worse code generation because the expression is left as = "X - (X - Y)". An additional subtraction pattern of "X - (X - Y) --> Y" was= added to this patch to avoid this regression. > > > > I also didn't remove the existing pattern because it triggered in more = cases than the new pattern because of a tree_invariant_p check that's inser= ted by genmatch for the new pattern. > > Yes, we do not handle using Y multiple times when it might contain > side-effects in GENERIC folding > (comments in genmatch suggest we can use save_expr but we don't > implement this [anymore]). > > On GIMPLE there's also the issue that your new pattern creates a > complex expression which > makes it failed to be used by value-numbering for example where the > old pattern was OK > (eventually, if no conversion was required). > > So indeed it looks OK to preserve both. > > I wonder why you needed the > > +/* X - (X - Y) --> Y */ > +(simplify > + (minus (convert1? @0) (convert2? (minus @@0 @1))) > + (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) && > TYPE_OVERFLOW_UNDEFINED(type)) > + (convert @1))) > > pattern since it should be handled by > > /* Match patterns that allow contracting a plus-minus pair > irrespective of overflow issues. */ > /* (A +- B) - A -> +- B */ > /* (A +- B) -+ B -> A */ > /* A - (A +- B) -> -+ B */ > /* A +- (B -+ A) -> +- B */ > > in particular > > (simplify > (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1))) > (view_convert @1)) > > if there's supported cases missing I'd rather extend this pattern than > replicating it. > > +/* X * (Y / X) is the same as Y - (Y % X). */ > +(simplify > + (mult:c (convert1? @0) (convert2? (trunc_div @1 @@0))) > + (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) > + (minus (convert @1) (convert (trunc_mod @1 @0))))) > > note that if you're allowing vector types you have to use > (view_convert ...) in the > transform and you also need to make sure that the target can expand > the modulo - I suspect that's an issue with the existing pattern as well. > I don't know of any vector ISA that supports modulo (or integer > division, that is). > Restricting the patterns to integer types is probably the most > sensible solution. > > Thanks, > Richard. > > > I verified that all "make -k check" tests pass when targeting x86_64-pc= -linux-gnu. > > > > 2021-03-31 Victor Tong > > > > gcc/ChangeLog: > > > > * match.pd: Two new patterns: One to optimize division followed= by multiply and the other to avoid a regression as explained above > > > > gcc/testsuite/ChangeLog: > > > > * gcc.dg/tree-ssa/20030807-10.c: Update existing test to look f= or a subtraction because a shift is no longer emitted > > * gcc.dg/pr95176.c: New test to cover optimizing division follo= wed by multiply > > > > I don't have write access to the GCC repo but I've completed the FSF pa= perwork as I plan to make more contributions in the future. I'm looking for= a sponsorship from an existing GCC maintainer before applying for write ac= cess. > > > > Thanks, > > Victor