From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 123864 invoked by alias); 7 Jul 2015 09:35:04 -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 123848 invoked by uid 89); 7 Jul 2015 09:35:04 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-oi0-f51.google.com Received: from mail-oi0-f51.google.com (HELO mail-oi0-f51.google.com) (209.85.218.51) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Tue, 07 Jul 2015 09:34:58 +0000 Received: by oiyy130 with SMTP id y130so137326979oiy.0 for ; Tue, 07 Jul 2015 02:34:56 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.60.176.37 with SMTP id cf5mr3147039oec.19.1436261696334; Tue, 07 Jul 2015 02:34:56 -0700 (PDT) Received: by 10.76.115.167 with HTTP; Tue, 7 Jul 2015 02:34:56 -0700 (PDT) In-Reply-To: References: Date: Tue, 07 Jul 2015 09:35:00 -0000 Message-ID: Subject: Re: [PR25529] Convert (unsigned t * 2)/2 into unsigned (t & 0x7FFFFFFF) From: Richard Biener To: GCC Patches Cc: "Hurugalawadi, Naveen" Content-Type: text/plain; charset=UTF-8 X-IsSubscribed: yes X-SW-Source: 2015-07/txt/msg00453.txt.bz2 On Tue, Jul 7, 2015 at 11:24 AM, Marc Glisse wrote: > On Tue, 7 Jul 2015, Richard Biener wrote: > >> On Tue, Jul 7, 2015 at 8:06 AM, Marc Glisse wrote: >>> >>> On Tue, 7 Jul 2015, Hurugalawadi, Naveen wrote: >>> >>>> Please find attached the patch "PR25529.patch" that converts the >>>> pattern:- >>>> (unsigned * 2)/2 is into unsigned &0x7FFFFFFF >>> >>> >>> >>> +/* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */ >>> +(for div (trunc_div ceil_div floor_div round_div exact_div) >>> + (simplify >>> + (div (mult @0 INTEGER_CST@1) INTEGER_CST@1) >>> >>> You don't need to repeat INTEGER_CST, the second time @1 is enough. >>> >>> + (with { tree n2 = build_int_cst (TREE_TYPE (@0), >>> + wi::exact_log2 (@1)); } >>> + (if (TYPE_UNSIGNED (TREE_TYPE (@0))) >>> + (bit_and @0 (rshift (lshift { build_minus_one_cst (TREE_TYPE (@0)); } >>> + { n2; }) { n2; })))))) >>> >>> What happens if you write t*3/3? >> >> >> Huh, and you posted this patch twice? See my reply to the other copy >> for the correctness issues and better handling of exact_div > > > They are not the same, one is for left shifts and the other one for right > shifts. And that makes a big difference: in t*c/c, the division is always > exact, so all divisions are equivalent. This is not the case for t/c*c. Ah, sorry. Still the same comment for computing the constant and placing of the 'with' applies. For signed types with TYPE_OVERFLOW_UNDEFINED you can simply cancel the operation (even for non-power-of-two multipliers). In fold-const.c extract_muldiv contains magic to handle this kind of cases. Otherwise for signed division (only the sign of the division matters, so you can probably ignore sign-changing conversions of the multiplication result) you can simplify it to a sign-extension from bit precision - log2 with the proposed introduction of a SEXT_EXPR (see other thread about type promotion). Richard. > -- > Marc Glisse