From patchwork Fri Jun 26 16:29:40 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 488926 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3F029140284 for ; Sat, 27 Jun 2015 02:29:55 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=F44AQMba; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=K0IjGyJ9zNLrozqoRj46ktCAFWAJAXuLsTcE5N/ybcV662lqDWva/ gtx6YIK51Lic1ONgjkN9l4Rc7YwNO1BzZFplDdHEZOsuhS+79MFlNddaKD0DoNxW GPLP9UezVM6G+UqMgAJ8LqVASAhkaefjhCOvTADlWv+MqhPbUNq+c8= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=hOYTJYSN4HMUUPY48f90ep+5tdk=; b=F44AQMbaNZr95aRRE+EE BNf8bogqQ9bCyHaJCGwuOWdt+RDOZmbRQnx7lVq+0dpF/uwI8Jm8BGBSRhuBQAcR P8SWZ+N3pxL4C/JkBnXlB6mVme/gvdr2HTanFolhYtFNOHDELhezW8iPbLJIVwIN +CPk0cHftStLUNpwYskCFzQ= Received: (qmail 49221 invoked by alias); 26 Jun 2015 16:29:46 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 49209 invoked by uid 89); 26 Jun 2015 16:29:45 -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, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Fri, 26 Jun 2015 16:29:45 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id EB7BB369F31; Fri, 26 Jun 2015 16:29:43 +0000 (UTC) Received: from redhat.com (ovpn-204-67.brq.redhat.com [10.40.204.67]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t5QGTefF025936 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO); Fri, 26 Jun 2015 12:29:43 -0400 Date: Fri, 26 Jun 2015 18:29:40 +0200 From: Marek Polacek To: GCC Patches , Richard Biener Subject: [PATCH] Move X - (X / Y) * Y folding to match.pd Message-ID: <20150626162940.GI10139@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) This is an attempt to move one pattern from fold-const.c to match.pd. It ought to be 1:1, but is not, e.g. with this patch we won't fold e.g. int f (int a, int b) { return a - (unsigned) ((a / b) * b) } anymore, but we're able to fold int ff (int a, unsigned int b) { return a - ((a / b) * b); } and fold-const.c is not. I played around with converts, but didn't find anything that would work well. Any suggestions how to make this pattern better? More to come... Bootstrapped/regtested on x86_64-linux, ok for trunk? 2015-06-26 Marek Polacek * fold-const.c (fold_binary_loc): Move X - (X / Y) * Y -> X % Y to ... * match.pd: ... pattern here. Marek diff --git gcc/fold-const.c gcc/fold-const.c index 6f12dd0..01e3983 100644 --- gcc/fold-const.c +++ gcc/fold-const.c @@ -10509,19 +10509,6 @@ fold_binary_loc (location_t loc, fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0))); - /* X - (X / Y) * Y is X % Y. */ - if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) - && TREE_CODE (arg1) == MULT_EXPR - && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR - && operand_equal_p (arg0, - TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0) - && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1), - TREE_OPERAND (arg1, 1), 0)) - return - fold_convert_loc (loc, type, - fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0), - arg0, TREE_OPERAND (arg1, 1))); - if (! FLOAT_TYPE_P (type)) { /* Fold A - (A & B) into ~B & A. */ diff --git gcc/match.pd gcc/match.pd index b2f8429..2bc158b 100644 --- gcc/match.pd +++ gcc/match.pd @@ -238,6 +238,12 @@ along with GCC; see the file COPYING3. If not see && tree_nop_conversion_p (type, TREE_TYPE (@1))) (trunc_mod @0 (convert @1)))) +/* X - (X / Y) * Y is the same as X % Y. */ +(simplify + (minus @0 (mult (trunc_div @0 @1) @1)) + (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) + (trunc_mod @0 @1))) + /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR, i.e. "X % C" into "X & (C - 1)", if X and C are positive. Also optimize A % (C << N) where C is a power of 2,