From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15582 invoked by alias); 7 Jul 2015 09:08:28 -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 15569 invoked by uid 89); 7 Jul 2015 09:08:27 -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-oi0-f44.google.com Received: from mail-oi0-f44.google.com (HELO mail-oi0-f44.google.com) (209.85.218.44) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Tue, 07 Jul 2015 09:08:26 +0000 Received: by oiaf66 with SMTP id f66so105113089oia.3 for ; Tue, 07 Jul 2015 02:08:24 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.202.49.212 with SMTP id x203mr2921723oix.81.1436260104765; Tue, 07 Jul 2015 02:08:24 -0700 (PDT) Received: by 10.76.115.167 with HTTP; Tue, 7 Jul 2015 02:08:24 -0700 (PDT) In-Reply-To: References: Date: Tue, 07 Jul 2015 09:08:00 -0000 Message-ID: Subject: Re: [PR25530] Convert (unsigned t / 2) * 2 into (unsigned t & ~1) 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-07/txt/msg00450.txt.bz2 On Tue, Jul 7, 2015 at 6:52 AM, Hurugalawadi, Naveen wrote: > Hi, > > Please find attached the patch PR25530.patch that converts the pattern:- > (unsigned / 2) * 2 is into (unsigned & ~1). > > Please review and let me know if its okay. For EXACT_DIV fold-const.c has /* ((T) (X /[ex] C)) * C cancels out if the conversion is sign-changing only. */ if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == EXACT_DIV_EXPR && operand_equal_p (arg1, TREE_OPERAND (arg0, 1), 0)) return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); we know the remainder is zero for EXACT_DIV. It also gives hints that a sign-changing conversion is ok. +/* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */ +/* PR25530. */ +(for div (trunc_div ceil_div floor_div round_div exact_div) + (simplify + (mult (div @0 INTEGER_CST@1) INTEGER_CST@1) + (with { tree n2 = build_int_cst (TREE_TYPE (@0), + wi::exact_log2 (@1)); } + (if (TYPE_UNSIGNED (TREE_TYPE (@0))) + (bit_and @0 (lshift (rshift { build_minus_one_cst (TREE_TYPE (@0)); } + { n2; }) { n2; })))))) you should move the (with inside the (if to save work if the type is not unsigned. Also you are using wi::exact_log2 without checking whether @1 was a power of two (I think exact_log2 returns -1 in this case). Then expressing ~1 with the result expression is really excessive - you should simply build this with @1 - 1 if @1 is a power of two. So please handle exact_div differently, like fold-const.c does. Also I am not sure ceil_div and floor_div can be handled this way. (5 /[ceil] 2) * 2 == 6 but you compute it as 4. So I am only convinced trunc_div works this way. Thanks, Richard. > Regression tested on AARH64 and x86_64. > > Thanks, > Naveen > > gcc/testsuite/ChangeLog: > > 2015-07-07 Naveen H.S > > PR middle-end/25530 > * gcc.dg/pr25530.c: New test. > > gcc/ChangeLog: > > 2015-07-07 Naveen H.S > > PR middle-end/25530 > * match.pd (mult (div @0 INTEGER_CST@1) INTEGER_CST@1) : > New simplifier.