From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 103552 invoked by alias); 8 Oct 2015 13:39:17 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 103543 invoked by uid 89); 8 Oct 2015 13:39:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.6 required=5.0 tests=AWL,BAYES_50,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-yk0-f176.google.com Received: from mail-yk0-f176.google.com (HELO mail-yk0-f176.google.com) (209.85.160.176) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Thu, 08 Oct 2015 13:39:15 +0000 Received: by ykft14 with SMTP id t14so48754695ykf.0 for ; Thu, 08 Oct 2015 06:39:13 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.129.125.6 with SMTP id y6mr5228015ywc.5.1444311553371; Thu, 08 Oct 2015 06:39:13 -0700 (PDT) Received: by 10.37.93.136 with HTTP; Thu, 8 Oct 2015 06:39:13 -0700 (PDT) In-Reply-To: References: Date: Thu, 08 Oct 2015 13:39:00 -0000 Message-ID: Subject: Re: Move some bit and binary optimizations in simplify and match From: Richard Biener To: "Hurugalawadi, Naveen" Cc: "gcc-patches@gcc.gnu.org" Content-Type: text/plain; charset=UTF-8 X-IsSubscribed: yes X-SW-Source: 2015-10/txt/msg00834.txt.bz2 On Wed, Oct 7, 2015 at 11:54 AM, Hurugalawadi, Naveen wrote: > Hi, > > Please find attached the patch that moves some more patterns from > fold-const using simplify and match. > > Please review the patch and let me know if any modifications are required. +/* Fold X + (X / CST) * -CST to X % CST. */ +(simplify + (plus @0 (mult:s (trunc_div:s @0 INTEGER_CST@1) (negate @1))) that's a bit too literal -- (negate @1) won't match for -@1 + (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) + (trunc_mod @0 @1))) +/* Fold (A & ~B) - (A & B) into (A ^ B) - B. */ +(simplify + (minus (bit_and:s @0 (bit_not:s @1)) (bit_and:s @0 @1)) + (if (! FLOAT_TYPE_P (type)) + (minus (bit_xor @0 @1) @1))) Likewise the fold code handles both constant and non-constant B. To mimic this you need a second pattern for the constant case or add a predicate matching @1 with ~@1. +/* (-A) * (-B) -> A * B */ +(simplify + (mult:c (negate @0) (negate @1)) + (mult @0 @1)) the fold-const.c code handles sign-conversions around the negates so you should add (convert?)s around them and verify useless_type_conversions. +/* Fold (a * (1 << b)) into (a << b) */ +(simplify + (mult:c @0 (lshift integer_onep@1 @2)) + (if (! FLOAT_TYPE_P (type)) + (lshift @0 @2))) Likewise (sign-conversion on the lshift). Though I'm not sure this won't trap ubsan for signed left-shift of negative values. +/* Fold (C1/X)*C2 into (C1*C2)/X. */ +(simplify + (mult (rdiv REAL_CST@0 @1) REAL_CST@2) + (if (FLOAT_TYPE_P (type) + && flag_associative_math) + (rdiv (mult @0 @2) @1))) the fold-const.c code avoids the transform if @0 * @2 doesn't simplify (nans/infs and flag combos). Not sure if we care though. +/* Simplify (X & ~Y) | (~X & Y) is X ^ Y. */ +(simplify + (bit_ior (bit_and:s @0 (bit_not:s @1)) (bit_and:s (bit_not:s @0) @1)) + (bit_xor @0 @1)) fold again handles also constants for X and Y. I suggest to re-use the matching predicate you need to add for the above ~ pattern. fold also handles sign-converted bit-ands. +/* Simplify ~X & X as zero. */ +(simplify + (bit_and:c @0 (bit_not:s @0)) + { build_zero_cst (type); }) I was sure we already have this... looks I was wrong. Again fold handles sign-conversions on @0 resp. the bit_not. +/* Simplify (X == 0) & X as zero. */ +(simplify + (bit_and:c @0 (eq @0 integer_zerop@1)) + @1) I think we have this one see logical_inverted_value and uses: (simplify (bit_and:c @0 (logical_inverted_value @0)) { build_zero_cst (type); }) +/* Fold X & (X ^ Y) as X & ~Y. */ +(simplify + (bit_and @0 (bit_xor:s @0 @1)) + (bit_and @0 (bit_not @1))) + +/* Fold X & (Y ^ X) as ~Y & X. */ +(simplify + (bit_and @0 (bit_xor:s @1 @0)) + (bit_and (bit_not @1) @0)) add :c on the bit_and and the bit_xor and then merge the patterns. Thanks, Richard. > Tested the patch on X86 without any regressions. > > Thanks, > Naveen > > ChangeLog > > 2015-10-07 Naveen H.S > > * fold-const.c (fold_binary_loc) : Move X + (X / CST) * -CST -> > X % CST to match.pd. > Move Fold (A & ~B) - (A & B) into (A ^ B) - B to match.pd. > Move (-A) * (-B) -> A * B to match.pd. > Move (a * (1 << b)) is (a << b) to match.pd. > Move convert (C1/X)*C2 into (C1*C2)/X to match.pd. > Move (X & ~Y) | (~X & Y) is X ^ Y to match.pd. > Move ~X & X, (X == 0) & X, and !X & X are zero to match.pd. > Move X & ~X , X & (X == 0), and X & !X are zero to match.pd. > Move Fold X & (X ^ Y) as X & ~Y to match.pd. > Move Fold X & (Y ^ X) as ~Y & X to match.pd. > > * match.pd (plus @0 (mult:s (trunc_div:s @0 INTEGER_CST@1) > (negate @1))): New simplifier. > (minus (bit_and:s @0 (bit_not:s @1)) (bit_and:s @0 @1)) : > New simplifier. > (mult:c @0 (lshift integer_onep@1 @2)): New simplifier. > (mult:c (plus @0 @0) INTEGER_CST@1): New simplifier. > (mult (rdiv REAL_CST@0 @1) REAL_CST@2): New simplifier. > (bit_ior (bit_and:s @0 (bit_not:s @1)) (bit_and:s (bit_not:s @0) @1)) > : New simplifier. > (bit_and:c @0 (bit_not:s @0)): New simplifier. > (bit_and:c @0 (eq @0 integer_zerop@1)): New simplifier. > (bit_and @0 (bit_xor:s @0 @1)): New simplifier. > (bit_and @0 (bit_xor:s @1 @0)): New simplifier. > (mult:c (negate @0) (negate @1)): New simplifier.