From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 61803 invoked by alias); 30 Apr 2015 07:17:30 -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 61793 invoked by uid 89); 30 Apr 2015 07:17:29 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY,T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mail2-relais-roc.national.inria.fr Received: from mail2-relais-roc.national.inria.fr (HELO mail2-relais-roc.national.inria.fr) (192.134.164.83) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (CAMELLIA256-SHA encrypted) ESMTPS; Thu, 30 Apr 2015 07:17:28 +0000 Received: from ip-205.net-81-220-140.rev.numericable.fr (HELO laptop-mg.local) ([81.220.140.205]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 30 Apr 2015 09:17:25 +0200 Date: Thu, 30 Apr 2015 07:28:00 -0000 From: Marc Glisse Reply-To: gcc-patches@gcc.gnu.org To: Jeff Law cc: gcc-patches@gcc.gnu.org Subject: Re: More type narrowing in match.pd In-Reply-To: <5541A704.3070502@redhat.com> Message-ID: References: <5541A704.3070502@redhat.com> User-Agent: Alpine 2.11 (DEB 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-SW-Source: 2015-04/txt/msg01972.txt.bz2 On Wed, 29 Apr 2015, Jeff Law wrote: > This is an incremental improvement to the type narrowing in match.pd. It's > largely based on the pattern I added to fix 47477. > > Basically if we have > > (bit_and (arith_op (convert A) (convert B)) mask) > > Where the conversions are widening and the mask turns off all bits outside > the original types of A & B, then we can turn that into > > (bit_and (arith_op A B) mask) > > We may need to convert A & B to an unsigned type with the same > width/precision as their original type, but that's still better than a > widening conversion. > > Bootstrapped and regression tested on x86_64-linux-gnu. > > OK for the trunk? +/* This is another case of narrowing, specifically when there's an outer + BIT_AND_EXPR which masks off bits outside the type of the innermost + operands. Like the previous case we have to convert the operands + to unsigned types to avoid introducing undefined behaviour for the + arithmetic operation. */ +(for op (minus plus) No mult? or widen_mult with a different pattern? (maybe that's already done elsewhere) + (simplify + (bit_and (op (convert@2 @0) (convert@3 @1)) INTEGER_CST@4) Maybe op@5 and then test single_use on @5? If I compute something, and before using it I test if the result is odd, I may not want to recompute it. + (if (INTEGRAL_TYPE_P (type) Can this be false, or is it for documentation? + /* We check for type compatibility between @0 and @1 below, + so there's no need to check that @1/@3 are integral types. */ + && INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && INTEGRAL_TYPE_P (TREE_TYPE (@2)) + /* The precision of the type of each operand must match the + precision of the mode of each operand, similarly for the + result. */ A nicely named helper that does this test would be cool. Every time I see it I have to think again why it is necessary, and if there was a function, I could refer to the comment above its definition ;-) + && (TYPE_PRECISION (TREE_TYPE (@0)) + == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0)))) + && (TYPE_PRECISION (TREE_TYPE (@1)) + == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1)))) + && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type)) + /* The inner conversion must be a widening conversion. */ + && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0)) + && ((GENERIC + && (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) + == TYPE_MAIN_VARIANT (TREE_TYPE (@1)))) + || (GIMPLE + && types_compatible_p (TREE_TYPE (@0), TREE_TYPE (@1)))) We don't need to be that strict, but this probably covers the most common case. + && (tree_int_cst_min_precision (@4, UNSIGNED) + <= TYPE_PRECISION (TREE_TYPE (@0)))) + (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) + (with { tree ntype = TREE_TYPE (@0); } + (convert (bit_and (op @0 @1) (convert:ntype @4))))) + (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } + (convert (bit_and (op (convert:utype @0) (convert:utype @1)) + (convert:utype @4))))))) -- Marc Glisse